JavaScript client verification through RegExp

Source: Internet
Author: User

1. LetOnly numbers are allowed in the text box.Use the text box control of asp.net mvc3.0.

@Html.TextBox("txt",null, new {@style="width:300;",onkeypress="return RegValidateIsDigit(event)" })

You can see that the onkeypress event is registered in the text box. when you enter a character in the text box and press the keyboard, the JavaScript function is triggered.

<Script type = "text/javascript"> function RegValidateIsDigit (e) {var KeyChar; debugger; /// judge the browser if (window. event) // IE {KeyChar = e. keyCode;} else if (e. which) // {KeyChar = e. which;} var str = String. fromCharCode (KeyChar); // returns regIsDigit (str) through UniCode encoding;} function regIsDigit (fData) {// define a regular expression to match var reg = new RegExp ("^ [0-9] $"); return (reg. test (fData) ;}</script>

First, judge the browser and handle the compatibility. Then use String. formCharCode (KeyChar) to find the corresponding characters

Finally, in the regIsDigit function

Define Regular Expressions for matching

var reg = new RegExp("^[0-9]$");

Because the value is 0-9, it is equivalent to \ d, that is

var reg = new RegExp("\\d$");

A regular expression is also defined as a character that contains a slash (/). Therefore, JavaScript may contain the following code:

var reg=/\d$/;

The test function is also used to check whether the specified string exists. Common functions include exec match search replace split.

If you understand the first one, you only need to apply the regular expression.

2.The text box can only contain Chinese characters.

    function RegValidateIsChinese(str) {        //var reg = new RegExp("^[\u4e00-\u9fa5]+$");        var reg = /^[\u4E00-\u9FA5]+$/;        var str=document.getElementById("text").value;        return (reg.test(str));    }

If RegValidateIsChinese ("input string") is a Chinese character, true is returned. If it is not all Chinese characters, false is returned.

3.Determination of email input format

Function RegValidateIsEmail (str) {// var reg =/^ ([a-zA-Z0-9 _-]) + @ ([a-zA-Z0-9 _-]) + ((\. [a-zA-Z0-9 _-] {2, 3}) {1, 2}) $/; var reg =/^ \ w + (-\ w +) | (\. \ w +) * @ {1} \ w + \. {1} \ w {2, 4 }(\. {0, 1} \ w {2}) {0, 1}/ig; if (reg. test (str) {alert ("email") ;}else {alert ("Incorrect format ");}}

Both definitions can be used for preliminary testing.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.