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.