在Android中向注册表单添加验证

时间:2014-07-14 04:43:24

标签: android validation android-layout android-intent android-activity

我有一份注册表格,我需要在提交前进行验证。表单包含以下字段:名称,名字,地址,密码,用户名和密码。我需要名称有一个值,密码应至少为7个数字,密码至少为6个字符。 如何为同一个添加验证。请帮助...

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;

    }




}

2 个答案:

答案 0 :(得分:0)

试试这个:

对于Pincode验证:

else if (postalcode.length() != 7) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter your 7 digits postalcode", Toast.LENGTH_SHORT).show();
            } 

密码:

else if (password.length() < 6) {
                invalid = true;
                Toast.makeText(getApplicationContext(),
                        "Please enter atleast 6 digits password", Toast.LENGTH_SHORT).show();
            } 

将输入类型设为xml中的数字,以获取密码编辑文本

as:android:inputType="number"android:inputType="phone"

答案 1 :(得分:0)

试试这个..

对于.trim()字段中的每个获取文字,请使用EditText,如下所示。

String postalcode = mPostalcode.getText().toString().trim();
String password = mpass.getText().toString().trim();

为您的邮政编码密码检查String 长度,如下所示

if(!(password.length() >= 6)){
       Toast.makeText(getApplicationContext(), "password must be at least 6 characters", Toast.LENGTH_SHORT).show();
}else if(postalcode.length() != 7){
       Toast.makeText(getApplicationContext(), "postalcode must be 7 numbers", Toast.LENGTH_SHORT).show();
}