I have a registration form which I need to validate before submit. The form has the following fields:name,firstname,address,pin code,username and password. I need the name to have a value,pin number should be numbers at least 7 numbers and the password to be at least 6 characters.
How to add validations for the same.Please Help...
Registartion.java
package com.example.app;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class Registration1 extends Activity implements OnClickListener,
OnItemSelectedListener{
private Button mSubmit;
private Button mCancel;
private EditText mGname;
private EditText mFname;
private EditText mMname;
private EditText mAddress1;
private EditText mAddress2;
private EditText mCityvillage;
private EditText mStateprovince;
private EditText mCountry;
private EditText mPostalcode;
private EditText mAge;
private EditText mBirthdate;
private Spinner mGender;
private EditText mUsername;
private EditText mpass;
private String Gen;
protected static DBHelper1 DB1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration1);
// Assignment of UI fields to the variables
mSubmit = (Button) findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
mCancel = (Button) findViewById(R.id.cancel);
mCancel.setOnClickListener(this);
mGname = (EditText) findViewById(R.id.egname);
mFname = (EditText) findViewById(R.id.efname);
mMname = (EditText) findViewById(R.id.eMname);
mAddress1 = (EditText) findViewById(R.id.eaddress1);
mAddress2 = (EditText) findViewById(R.id.eaddress2);
mCityvillage = (EditText) findViewById(R.id.ecityvillage);
mStateprovince = (EditText) findViewById(R.id.estateprovince);
mCountry = (EditText) findViewById(R.id.ecountry);
mPostalcode = (EditText) findViewById(R.id.epostalcode);
mAge = (EditText) findViewById(R.id.eage);
mBirthdate = (EditText) findViewById(R.id.ebirthdate);
mGender = (Spinner) findViewById(R.id.spinner1);
mUsername = (EditText) findViewById(R.id.eusername);
mpass = (EditText) findViewById(R.id.epass);
// Spinner method to read the on selected value
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
android.R.layout.simple_spinner_item, new State[] {
new State("Male"), new State("Female") });
mGender.setAdapter(spinnerArrayAdapter);
mGender.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cancel:
Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);
// finish();
break;
case R.id.submit:
System.out.println("rEGISTER BUTTON CLICK");
String gname = mGname.getText().toString();
String fname = mFname.getText().toString();
String mname = mMname.getText().toString();
String address1 = mAddress1.getText().toString();
String address2 = mAddress2.getText().toString();
String cityvillage = mCityvillage.getText().toString();
String stateprovince = mStateprovince.getText().toString();
String country = mCountry.getText().toString();
String postalcode = mPostalcode.getText().toString();
String age = mAge.getText().toString();
String birthdate = mBirthdate.getText().toString();
Gen = mGender.getSelectedItem().toString();
String username = mUsername.getText().toString();
String password = mpass.getText().toString();
boolean invalid = false;
if (gname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Givenname",
Toast.LENGTH_SHORT).show();
} else
if (fname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your firstname", Toast.LENGTH_SHORT)
.show();
} else
if (mname.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your middlename", Toast.LENGTH_SHORT)
.show();
} else
if (address1.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your address1", Toast.LENGTH_SHORT)
.show();
} else if (address2.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your address2", Toast.LENGTH_SHORT)
.show();
} else if (cityvillage.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your cityvillage", Toast.LENGTH_SHORT).show();
} else if (stateprovince.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your stateprovince", Toast.LENGTH_SHORT).show();
} else if (country.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your country", Toast.LENGTH_SHORT).show();
} else if (postalcode.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your postalcode", Toast.LENGTH_SHORT).show();
} else if (age.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your age", Toast.LENGTH_SHORT).show();
}else if (birthdate.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your birthdate", Toast.LENGTH_SHORT).show();
}else if (username.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your Username", Toast.LENGTH_SHORT).show();
} else if (password.equals("")) {
invalid = true;
Toast.makeText(getApplicationContext(),
"Please enter your password", Toast.LENGTH_SHORT).show();
}
else if (invalid == false)
{
addEntry(gname, fname, mname, address1, address2, cityvillage,
stateprovince, country, postalcode, age, birthdate, Gen,
username,password);
Intent i_register = new Intent(Registration1.this,
LoginActivity1.class);
startActivity(i_register);
// finish();
}
break;
}
}
public void onDestroy() {
super.onDestroy();
//DB1.close();
}
private void addEntry(String gname,String fname,String mname,String address1,String address2,String cityvillage,String
stateprovince,String country,String postalcode,String age,String birthdate,String Gen,String username,String password)
{
SQLiteDatabase db1 = DB1.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put("id", 2);
values.put("givenname", gname);
values.put("firstname", fname);
values.put("middlename", mname);
values.put("address1", address1);
values.put("address2", address2);
values.put("cityvillage", cityvillage);
values.put("stateprovince", stateprovince);
values.put("country", country);
values.put("postalcode", postalcode);
values.put("age", age);
values.put("birthdate", birthdate);
values.put("gender", Gen);
values.put("username", username);
values.put("password", password);
try {
long rowId = db1.insert(DBHelper1.DATABASE_TABLE_NAME1, null, values);
System.out.println("rowId: "+rowId);
Toast.makeText(getApplicationContext(),
"your details submitted Successfully...",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// Get the currently selected State object from the spinner
State st = (State) mGender.getSelectedItem();
// Show it via a toast
toastState("onItemSelected", st);
}
public void toastState(String name, State st) {
if (st != null) {
Gen = st.name;
// Toast.makeText(getBaseContext(), Gen, Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public static void setDB(DBHelper1 dB2) {
DB1=dB2;
}
}
See Question&Answers more detail:
os