AS3 validates form fields?

I wrote an AS3 script, I have 2 fields to validate, like email and name.

For email, I use:

function isValidEmail(Email:String):Boolean {
    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;    
    return emailExpression.test(Email); 
}

      

How about a name field? Can you show me some sample code?

EDIT:

The following are invalid:

  • space

  • between 4 - 20 characters

  • Alphanumeric characters only (special characters not allowed)

  • You need to start with the alphabet

0


source to share


2 answers


I think you probably want a function like this:

function isNameValid(firstname:String):Boolean
{
    var nameEx:RegExp = /^([a-zA-Z])([ \u00c0-\u01ffa-zA-Z']){4,20}+$/;
    return nameEx.test(firstname);
}

      

Shorthand for this regex:

  • [a-zA-Z] - checks if the first char is a normal letter.
  • [\ u00c0- \ u01ffa-zA-Z '] - Checks if all other characters are Unicode or space. So names like "Mc'Neelan" will work.
  • {4.20} . Verifies that the name is 4 to 20 characters long.


You can remove the space at the beginning of the middle section if you don't need spaces.

Hope this helps. here are my links:

+1


source


function isNameValid(firstname:String):Boolean
{
    var nameEx:RegExp = /^([a-zA-Z])([ \u00c0-\u01ffa-zA-Z']){4,20}+$/;
    return nameEx.test(firstname);
}


{4,20} instead {2,20}
Problem avoided for names like Ajit

      



+1


source







All Articles