2

I want to validate this NIC number in Android Java.

NICs=NIC.getText().toString();
if((nameS.equals("")||NICs.equals(""))||!(NICs.matches("/^[0-9]{9}[vVxX]$/")))
{
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

        // set title
        alertDialogBuilder.setTitle("Warning!");

        // set dialog message
        alertDialogBuilder
            .setMessage("Please fill the above feilds !")
            .setCancelable(false)

            .setNegativeButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

My NIC="896201328V" But it says NIC mismatches with the pattern given.

1 Answer 1

2

Remove refex delimiters / from your code so use:

NICs.matches("[0-9]{9}[vVxX]")

instead of:

NICs.matches("/^[0-9]{9}[vVxX]$/")

PS: Anchors are also not needed in String#matches method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.