Regular expression control input content, JS Regular verification method Daquan

Source: Internet
Author: User

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

  1. <div>
  2. <%--can only enter numbers, directly with the regular can be--%>
  3. <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') '/>
  4. <%--validation percent, using JS regular validation--%>
  5. <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') "onblur="checkpercent (This) "/>
  6. <%--Verify the phone number, using JS regular validation--%>
  7. <input type="text" onkeyup="This.value=this.value.replace (/[^0-9.] /g, ') "onblur="checkmobile (this.value) "/>
  8. </div>
  9. <!--JS Regular validation Display-
  10. <script type="Text/javascript" >
  11. //Verify percent
  12. function checkpercent (obj) {
  13. if (obj.Value! = "") {
  14. var str = obj.value;
  15. var pattern =/^ ([1-9]{1}[0-9]{0,1}| 0) (\.\d{1,2}) {0,1}$/;
  16. if (!str.match (pattern)) {
  17. Alert ("Malformed, valid format such as: 88.88% (integer up to two bits, decimal place up to two bits)");
  18. obj.value = "";
  19. Obj.focus ();
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. //Verification telephone
  26. function checkmobile (s) {
  27. if (Trim (s)! = "") {
  28. var regu =/^[1][3-8][0-9]{9}$/;
  29. var re = new RegExp (Regu);
  30. if (re.test (s)) {
  31. alert (s);
  32. return true;
  33. } Else {
  34. Alert ("Incorrect phone entry format");
  35. return false;
  36. }
  37. }
  38. }
  1. //Verification telephone
  2. var checkphone = function () {
  3. var $phone = $ ("#memberPhone");
  4. 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 () ))
  5. {
  6. Alert ("Warm tip: Please enter the correct phone number!") ");
  7. $phone. focus ();
  8. return;
  9. }
  10. }
  11. </script>
  12. <!--JS Trim () method --
  13. <script type="Text/javascript" >
  14. function trim (str) { //delete left and right spaces
  15. return Str.replace (/(^\s*) | ( \s*$)/g, "");
  16. }
  17. function LTrim (str) { //delete left space
  18. return Str.replace (/(^\s*)/g, "");
  19. }
  20. function RTrim (str) { //delete the right space
  21. return Str.replace (/(\s*$)/g, "");
  22. }
  23. </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

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.