views:

539

answers:

1

Hi,

I wrote a AS3 script, i have 2 fields to validate, i.e 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 name field? Can you show me some sample code?

EDIT:

Invalid are:

  • blank

  • between 4 - 20 characters

  • Alphanumeric only(special characters not allowed)

  • Must start with alphabet

A: 

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);
}

Rundown of that regular expression:

  • [a-zA-Z] - Checks if first char is a normal letter.
  • [ \u00c0-\u01ffa-zA-Z'] - Checks if all other chars are unicode characters or a space. So names like "Mc'Neelan" will work.
  • {4,20} - Makes sure the name is between 4 and 20 chars in length.

You can remove the space at the start of the middle part if you don't want spaces.

Hope this helps. here are my references:

TandemAdam