4041507 Regular expression controls input inputsApril 01, 2009 17:15:00Hits: 21747
Cannot enter Chinese
<input type= "text" name= "TextField" onkeyup= "This.value=this.value.replace (/[^/da-z_]/ig, ');" />
Only numbers and underscores can be entered
<input onkeypress= "Return (/[/d_]/.test (String.fromCharCode (Event.keycode))" style= "ime-mode:disabled"/>
Only numbers and decimal points can be entered
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled "/>
Allow only Chinese characters to be entered </br>
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" >
One is to allow only numbers and decimal points to be entered.
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled ">
Second, the judgment is more detailed, even 22. 2 That's not a number.
<script>
function Check () {
if (IsNaN (Tt.value))
{alert ("illegal character! ");
Tt.value= "";}
}
</script>
<input type= "text" Name= "tt" onkeyup= "check ();" >
The third, only allows the input of integers. In fact, we can also make some restrictions according to the third article extrapolate.
<script language=javascript>
function Onlynum ()
{
if (! ( EVENT.KEYCODE==46) &&! (event.keycode==8) &&! (event.keycode==37) &&! (event.keycode==39))
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Event.returnvalue=false;
}
</script><input onkeydown= "Onlynum ();" Style= "ime-mode:disabled>
The conclusion, in fact
Style= "ime-mode:disabled
This sentence is more practical. Turn off the IME. Save some people to open the full-width input number, the result input does not come in to find you kutianmalei, also blame you design bad.
Allow only numeric input
<input name= "username" type= "text" onkeyup= "Value=this.value.replace (//d+/g, ')" >
Only English letters, numbers, and underscores are allowed (the following two methods are implemented)
<input name= "username" type= "text" style= "ime-mode:disabled" >
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/w/.//]/ig, ')" >
Only English letters, numbers and &[email are allowed protected]
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/[email protected]&]|_/ig, ')" >
Allow only Chinese characters to be entered
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" >
44037245
1.JS Regular Validation Example
- <div>
- <%--can only enter numbers, directly with the regular can be--%>
- <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') '/>
- <%--validation percent, using JS regular validation--%>
- <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') "onblur="checkpercent (This) "/>
- <%--Verify the phone number, using JS regular validation--%>
- <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') "onblur="checkmobile (this.value) "/>
- </div>
- <!--JS Regular validation Display-
- <script type="Text/javascript" >
- //Verify percent
- function checkpercent (obj) {
- if (obj.Value! = "") {
- var str = obj.value;
- var pattern =/^ ([1-9]{1}[0-9]{0,1}| 0) (\.\d{1,2}) {0,1}$/;
- if (!str.match (pattern)) {
- Alert ("Malformed, valid format such as: 88.88% (integer up to two bits, decimal place up to two bits)");
- obj.value = "";
- Obj.focus ();
- return false;
- }
- }
- return true;
- }
- //Verification telephone
- function checkmobile (s) {
- if (Trim (s)! = "") {
- var regu =/^[1][3-8][0-9]{9}$/;
- var re = new RegExp (Regu);
- if (re.test (s)) {
- alert (s);
- return true;
- } Else {
- Alert ("Incorrect phone entry format");
- return false;
- }
- }
- }
- //Verification telephone
- var checkphone = function () {
- var $phone = $ ("#memberPhone");
- if (!/^ (13[0-9]| 14[0-9]| 15[0-9]| 16[0-9]| 17[0-9]| 18[0-9]) \d{8}$/i.test ($phone. Val () ))
- {
- Alert ("Warm tip: Please enter the correct phone number!") ");
- $phone. focus ();
- return;
- }
- }
- </script>
- <!--JS Trim () method --
- <script type="Text/javascript" >
- function trim (str) { //delete left and right spaces
- return Str.replace (/(^\s*) | ( \s*$)/g, "");
- }
- function LTrim (str) { //delete left space
- return Str.replace (/(^\s*)/g, "");
- }
- function RTrim (str) { //delete the right space
- return Str.replace (/(\s*$)/g, "");
- }
- </script>
2. The Regular validation example
Description: Regular expressions are typically used for two tasks: 1. Validation, 2. Search/Replace. When used for validation, it is often necessary to add ^ and $ respectively to match the entire string to be validated, or to add this qualification to search/replace depending on the requirements of the search, in addition to the possibility to add \b instead of ^ and $. The commonly used regular expressions listed in this table, except for each other, are not preceded by any qualification, and should be handled on your own.
| Description |
Regular Expressions |
| Web address (URL) |
[a-za-z]+://[^\s]* |
| IP addresses (IP address) |
((2[0-4]\d|25[0-5]| [01]?\d\d?] \.) {3} (2[0-4]\d|25[0-5]| [01]?\d\d?] |
| e-mail (email) |
\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * |
| QQ number |
[1-9]\d{4,} |
| HTML markup (contains content or self-closing) |
< (. *) (. *) >.*<\/\1>|< (. *) \/> |
| Password (consists of a number/uppercase/lowercase letter/punctuation, four must have, more than 8) |
(?=^. {8,}$) (? =.*\d) (? =.*\w+) (? =.*[a-z]) (? =.*[a-z]) (?!. *\n). *$ |
| Date (year-month-day) |
(\d{4}|\d{2})-((1[0-2)) | ( 0? [1-9])) -(([12][0-9]) | (3[01]) | (0?) [1-9])) |
| Date (month/day/year) |
((1[0-2]) | (0?) [1-9])) /(([12][0-9]) | (3[01]) | (0?) [1-9])) /(\d{4}|\d{2}) |
| Time (Hours: minutes, 24-hour system) |
(1|0?) [0-9]|2[0-3]):([0-5][0-9]) |
| Kanji (character) |
[\u4e00-\u9fa5] |
| Chinese and full-width punctuation (characters) |
[\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee] |
| Fixed telephone number in mainland China |
(\d{4}-|\d{3}-)? (\d{8}|\d{7}) |
| Mobile phone number in mainland China |
1\D{10} |
| China Mainland Postcode |
[1-9]\d{5} |
| China Mainland ID Number (15-bit or 18-bit) |
\D{15} (\d\d[0-9xx])? |
| Non-negative integers (positive integers or 0) |
\d+ |
| Positive integers |
[0-9]*[1-9][0-9]* |
| Negative integer |
-[0-9]*[1-9][0-9]* |
| Integer |
-?\d+ |
| Decimal |
(-?\d+) (\.\d+)? |
| Words that do not contain ABC |
\b (?! ABC) \w) +\b
|
3. Regular replacement string in the picture label Regular: <\s?img[^>]*>var contesstr = Contesstr.replace (/<\s?img[^>]*>/gi, "); 4. Regular match all P tag regular:<\s?p> can match <p> </p>
Regular expression control input content, JS Regular verification method Daquan