js common verify rule using regular expressions cheatsheet

When we design a form, the data must be verifyed before the user submit. Regular expressions can help us to do this task and impove the customer experience.

Here is a list of most used rules in a validator.


How to use 

Markup

<script>
    var inputValue = "",// 
        expr = /^[0-9]+$/; 
    if(expr.test(inputValue)){ // expr.exec(string)
        //allow
    }
    else{
        //not allow
    }
</script>


/^[0-9]*$/    // allow only numeric
/^d{n}$/      // only n digits, use occasion: Credit card number verify
/^d{n,}$/     // at least n number
/^[A-za-z]+$/ // matching only chars
/^[A-za-z0-9_-]{3,16}$/ 
              // Matching a username,length 3-16
/^[A-za-z0-9_-]{8,18}$/ 
              // Matching a password,length 8-18
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/
              // Matching an Email Address
/^([01][0-9][0-9]|2[0-4][0-9]|25[0-5])$/ 
              // Validating Numeric Ranges 000..255
/(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.
(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])/
              // Matching an IP Address
/^((d{3,4})|d{3,4}-)?d{7,8}$/
              // validation for telephone XXXX-XXXXXXX XXX-XXXXXXX
/^http://([w-]+.)+[w-]+(/[w-./?%&=]*)?$/
              // Validating URL
/< (S*?)[^>]*>.*?|< .*? />/
              // find out all html tags


date2019-03-23 16:00:01 From:www.jquerypluginhub.com Author:JPH
Related Tags:
Comments

Leave a Reply

Your email address will not be published. Required fields are marked *