2014-10-29 34 views
2

我想在Android Java中验证此NIC编号。在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(); 
       } 
      }); 

我NIC = “896201328V” 但它说NIC错配给出的格局。

回答

1

删除refex定界符/从您的代码,以便使用:

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

代替:

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

PS:也没有必要在String#matches方法锚。

+1

非常感谢。有用。 – Isuru 2014-10-29 10:17:13

+1

不客气,很高兴它解决了。 – anubhava 2014-10-29 10:17:49