1. Let the text box only allow input numbers, the use of asp.net mvc3.0 text box control
Copy Code code as follows:
@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, and the JavaScript function is triggered when you press the keyboard in a text box.
Copy Code code as follows:
<script type= "Text/javascript" >
function Regvalidateisdigit (e) {
var Keychar;
Debugger
Judge the browser
if (window.event)//ie
{
Keychar = E.keycode;
}
else if (E.which)///firefox, etc.
{
Keychar = E.which;
}
var str = string.fromcharcode (Keychar); Use Unicode encoding to find the appropriate character
return Regisdigit (str);
}
function Regisdigit (fdata) {
Define a regular match
var reg = new RegExp ("^[0-9]$");
Return (Reg.test (fdata));
}
</script>
First, judge the browser for processing compatibility. Then String.formcharcode (Keychar) to find the appropriate characters
Finally, in the function Regisdigit function
Define a regular match
Copy Code code as follows:
var reg = new RegExp ("^[0-9]$");
Because it's a value of 0-9, so it's equivalent to \d.
Copy Code code as follows:
var reg = new RegExp ("\\d$");
The direct amount of a regular expression is also defined as a character that is contained between a pair of slashes (/). Therefore, JavaScript may contain the following code:
Copy Code code as follows:
The test function is also used here: Check that the specified string exists. Also commonly used are the exec match search replace split and other functions.
If you understand the first then you just need to apply the right then you can use OH.
2. The text box only allows you to enter Chinese
Copy Code code as follows:
function Regvalidateischinese (str) {
var reg = new RegExp ("^[\u4e00-\u9fa5]+$");
var reg =/^[\u4e00-\u9fa5]+$/;
var Str=document.getelementbyid ("text"). Value;
Return (Reg.test (str));
}
Regvalidateischinese ("input string") is a Chinese character returns true, not all Chinese characters return false
3. The decision of the mailbox input format
Copy Code code as follows:
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 ("is the Mailbox");
}
else {
Alert ("wrong format");
}
}
Two definitions of preliminary tests are available.