Illep
Illep

Reputation: 16841

Error checking in my application

1.) I have a code script to check if the user has entered a correct email format;

if (val.search("[a-z]+@[a-z]+[.][a-z]+") == -1)
                          Ext.Msg.alert("Error", "Invalid e-mail address!!"); 

This code is incorrect as the username and domain name could take in Numbers as well. So how do i correct the code ?

2.) In the view, i have 2 fields; password and retype password. How do i check if these 2 are equal ?

{
                          xtype:'textfield',
                          name:'password',
                          label:'Password'
                          },
                          {
                          xtype:'textfield',
                          name:'rpassword',
                          label:'retype password'
                          }
                          ]
                   },
                   {
                   xtype:'button',
                   id:'pwdButton',
                   text:'register',
                   ui:'confirm'
}

3.) Is there a possibility to do Ajaxified error checking, meaning to pop up error messages as the user goes to the next field (without having to wait till he clicks the Register button)

Upvotes: 0

Views: 129

Answers (1)

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

  1. For performing the email validation, you can check out my below code. It also works out, if the username and domain name have Numbers.

    {
      xtype: 'emailfield',
      name: 'email',
      label: 'Email',
      id: 'emailId',
      placeHolder: '[email protected]',
      required: true,
      listeners: {
        blur: function(thisTxt, eventObj) {
                var emailValue = thisTxt.getValue();
                var atpos = emailValue.indexOf("@");
                var dotpos = emailValue.lastIndexOf(".");
                if (atpos<1 || dotpos<atpos+2 || dotpos+2>= emailValue.length) {
                    Ext.Msg.alert("Not a valid e-mail address");
                } else {
                    Ext.Msg.alert('Valid','You entered a valid email address');
                }
        }
       }
     }
    
  2. For checking if user retyped the same password correctly, you can check out the below code,'

     {
        xtype:'textfield',
        name:'password',
        id: 'pswd1',
        label:'Password'
     },
     {
        xtype:'textfield',
        name:'rpassword',
        id: 'pswd2',
        label:'retype password',
        listeners : {
            blur : function() {
               var prevPassword = Ext.getCmp('pswd1').getValue();
               var rePassword = Ext.getCmp('pswd2').getValue();
               if(prevPassword == rePassword) {
                    Ext.Msg.alert('Success','Passwords match');
               } else {
                    Ext.Msg.alert('Failed','Passwords entered do not match');
                    Ext.getCmp('pswd2').focus();
               }
           }
       }
     }
    
  3. meaning to pop up error messages as the user goes to the next field (without having to wait till he clicks the Register button)

You need to listen for the blur event of any form field. So, when that p'cular field looses focus, it will call the blur() function and then you can perform the validation w/o having to hit the Register button.

listeners : {
     blur : function() {
         // Validation code goes here .
         ....
         ....
     }
}

Upvotes: 2

Related Questions