【问题标题】:Android EditText Validation and RegexAndroid EditText 验证和正则表达式
【发布时间】:2015-01-31 16:03:27
【问题描述】:

我即将为 android 创建 editText 验证,我使用了 if..else 语句,但编码似乎不适用于我的应用程序...我想让所有字段都是强制性的,如果您将一个字段留空,您无法继续,我也对电话号码的验证感到好奇,因为我来自马来西亚,所以有效的电话号码是例如:0133999504,不允许使用“-”符号或任何字母..只有数字和“+”号有效..身份证号码也是一样,马来西亚的身份证号码就像身份证号码,我也想要“-”符号不允许只有字母和数字才有效.. 发生错误时如何使editText框边框突出显示?..下面是我的完整编码..有什么想法或解决方案吗?

package com.sor.communityrideattendance;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.widget.Toast;
//import android.view.MenuInflater;
//import android.view.Menu;
import android.view.MenuItem; 

public class RiderProfile extends Activity {


    // Progress Dialog
    private ProgressDialog pDialog;


    JSONParser jsonParser = new JSONParser();
    EditText inputfullname;
    EditText inputIC;
    EditText inputmobileNo;
    EditText inputemergencyContactName;
    EditText inputemergencyContactNo;
    String PhoneNo;
    String PhoneNo_PATTERN ="^[+]?[0-9]{10,13}$";
    //private static final String TAG_ID = "ID";

    // url to create new product
    private static String url_create_rider = "http://192.168.0.28/android_connect/create_rider.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rider_profile);

        // Edit Text
        inputfullname= (EditText) findViewById(R.id.fullname);
        inputIC= (EditText) findViewById(R.id.IC);
        inputmobileNo= (EditText) findViewById(R.id.mobileNo);
        inputemergencyContactName= (EditText) findViewById(R.id.emergencyContactName);
        inputemergencyContactNo= (EditText) findViewById(R.id.emergencyContactNo);
        /*Create button*/
        Button btnSave = (Button) findViewById(R.id.btnSave);

        // button click event
        btnSave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread

                //start
                if(inputfullname.getText().toString().equals(""))
                {
                    inputfullname.setError("Please Enter Your Name");
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                }
                else if(inputIC.getText().toString().equals("")) //IC
                    {
                    inputIC.setError("Please Enter Your IC Number");
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                } else if(inputmobileNo.getText().toString().equals("")) //mobileNo
                {
                    Pattern pattern = Pattern.compile(PhoneNo_PATTERN);
                    Matcher matcher = pattern.matcher(PhoneNo);
                    if (!matcher.matches()) 
                    {
                    inputmobileNo.setError("Please Enter Your Mobile No");
                    }
                    else{
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                    }
                }else if(inputemergencyContactName.getText().toString().equals("")) //emergencyName
                {
                    inputemergencyContactName.setError("Please enter Emergency Contact Name");  
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                }else if(inputemergencyContactNo.getText().toString().equals("")) //emergencyNo
                {
                    Pattern pattern = Pattern.compile(PhoneNo_PATTERN);
                    Matcher matcher = pattern.matcher(PhoneNo);
                    if (!matcher.matches()) 
                    {
                    inputemergencyContactNo.setError("Please enter Emergency Contact No");
                    }
                    else{
                    Toast.makeText(RiderProfile.this, "Please filled in All field", Toast.LENGTH_SHORT).show();
                    }
                }
                //validation end
                else {
                new CreateNewRider().execute();
                }
            }
        });
    }

                //end



    /**
     * Background Async Task to Create new product
     * */
    class CreateNewRider extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(RiderProfile.this);
            pDialog.setMessage("Create Rider..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating rides
         * */
        protected String doInBackground(String... args) {
            String fullname= inputfullname.getText().toString();
            String IC= inputIC.getText().toString();
            String mobileNo= inputmobileNo.getText().toString();
            String emergencyContactName= inputemergencyContactName.getText().toString();
            String emergencyContactNo= inputemergencyContactNo.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("fullname", fullname));
            params.add(new BasicNameValuePair("IC", IC));
            params.add(new BasicNameValuePair("mobileNo", mobileNo));
            params.add(new BasicNameValuePair("emergencyContactName", emergencyContactName));
            params.add(new BasicNameValuePair("emergencyContactNo", emergencyContactNo));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_rider,
                    "POST", params);

            // check log cat from response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), ListOfRider.class);
                    //Intent i = new Intent(getApplicationContext(), SuccessRidesProfile.class);
                    //in.putExtra(TAG_PID, pid);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.dismiss();
        }

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.items, menu);

        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        super.onOptionsItemSelected(item);
        final Context context = this;
        switch(item.getItemId()){
            case R.id.phone:

                Intent intent = new Intent(context, SubMenu.class);
                startActivity(intent);
                //Toast.makeText(getBaseContext(), "You selected Phone", Toast.LENGTH_SHORT).show();
                break;
        }

        return true;

    }
}

【问题讨论】:

    标签: android regex validation android-edittext


    【解决方案1】:

    您可以使用此库进行验证.. 一个简单的库,用于使用注释验证表单中的用户输入。

    https://github.com/inmite/android-validation-komensky

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2016-05-04
      • 2012-03-03
      相关资源
      最近更新 更多