RegExp is a testing pattern often used for validating form fields such as name, email, phone number etc. It is also possible to create custom RegExp patterns to stop particular words or numbers.
Here I will explain each
Using RegExp in both As3 and JavaScript uses relatively the same syntax.
JavaScript
var ck_name = /^[A-Za-z0-9 ]{3,20}$/; var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
AS3
var ck_name:RegExp = /^[A-Za-z0-9 ]{3,20}$/; var ck_email:RegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
To test if a string meets the criteria of a RegExp pattern we use test() which returns either true or false.
JavaScript
var ck_name = /^[A-Za-z0-9 ]{3,20}$/; var user_name = "john1983" ; if(ck_name.test(user_name)) { alert("username is acceptable") } else { alert("username cannot contain special characters") }
Custom RegExp
The below will return False in a browser popup because there is no ‘p’ in the name ‘john’
var my_custom_regExp = new RegExp("p") alert(my_custom_regExp.test("john"))
Leave A Comment