代码改变世界

AS3 Phone and Email Validation RegExp Snippet

2010-03-16 14:52  宝宝合凤凰  阅读(376)  评论(0编辑  收藏  举报

AS3 Phone and Email Validation RegExp Snippet

This is Brendan blogging live from Ribbit Developer Central, where apparently, everyone has been more nice than naughty this year.

This is for folks to use on their Flash projects, as Flex has a built in validation util, (you go Flex!).

 *** Disclaimer*** if you have a better RegExp, please share in the comments **** 

The first is RegExp validation for an email, returns true or false - 

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

 the the second is RegExp validation for an US phone number, returns true or false - 

private  function isValidPhoneNumber( str:String ):Boolean {                 
  var phoneRegExp:RegExp = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/i;   
                return phoneRegExp.test( str );        
    } 

This should come in handy for you folks that want to do some smart, client side checking of validity or log-in params.  Happy Holidays!