JavaScript-Regular Expressions

Source: Internet
Author: User

The approximate matching process for regular expressions is to take out the expression and compare the characters in the text, and if each character matches, the match succeeds; If a match is unsuccessful, the match fails.

Regular expressions are typically used to find matching strings in text. The number of words in Python is greedy by default (which may be the default non-greedy in a few languages), always trying to match as many characters as possible, and not greedy, instead, always trying to match as few characters as possible. For example: the regular expression "ab*" will find "abbb" if it is used to find "ABBBC". And if you use a non-greedy quantity word "ab*?", you will find "a".

var Pageindex=document.getelementbyid ("TextField"). Value;

if (!/^[0-9]+$/.test (PageIndex)) {
Alert ("Please enter a positive integer");
return false;
} else{
Alert ("The input is a positive integer");
}

/determine if the input content is empty
function IsNull () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length==0) {
Alert (' Sorry, the text box cannot be empty or blank! '); /Please change the text box to the name of the property you need to verify!
}
}

Determine whether the date type is a type of YYYY-MM-DD format
function IsDate () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Type that determines whether the date type is YYYY-MM-DD hh:mm:ss format
function Isdatetime () {
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/;
var r = Str.match (reg);
if (r==null)
Alert (' Sorry, the date you entered is not in the correct format! '); Please change "date" to the name of the property you need to verify!
}
}

Determine whether the date type is a type of HH:MM:SS format
function Istime ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^ ((20|21|22|23|[ 0-1]\d) \:[0-5][0-9]) (\:[0-5][0-9])? $/
if (!reg.test (str)) {
Alert ("Sorry, the date you entered is not in the correct format!"); /Please change the "date" to the name of the property you need to verify!
}
}
}

Determine if the character entered is an English letter
function Isletter ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z]+$/;
if (!reg.test (str)) {
Alert ("Sorry, the English letter type you typed is not properly formatted!"); /Please change the "English letter type" to the name of the property you need to verify!
}
}
}

Determines whether the input character is an integer
function Isinteger ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {
Alert ("Sorry, the integer type you entered is not formatted correctly!"); /Please replace the "integer type" with the name of the property you want to verify!
}
}
}

Determines whether the input character is double precision
Function Isdouble (val)
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
Reg=/^[-\+]?\d+ (\.\d+) $/;
if (!reg.test (str)) {
Alert ("Sorry, the double type you entered is not in the correct format!"); /Please replace "double type" with the name of the property you want to verify!
}
}
}


Determine if the character entered is: a-z,a-z,0-9
function isstring ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[a-za-z0-9_]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determines whether the character entered is in Chinese
function Ischinese ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[\u0391-\uffe5]+$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input email format is correct
function Isemail ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Determine if the input ZIP code (six-bit only) is correct
function Iszip ()
{
var str = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^\d{6}$/;
if (!reg.test (str)) {
Alert ("Sorry, you typed the string type incorrectly!"); /Please replace "string type" with the name of the property you want to verify!
}
}
}

Judging the number entered is not greater than a specific number
function MaxValue ()
{
var val = document.getElementById (' str '). Value.trim ();
if (str.length!=0) {
reg=/^[-+]?\d*$/;
if (!reg.test (str)) {//Determines whether a number type
if (Val>parseint (' 123 '))//"123" for the maximum value set for itself
{
Alert (' Sorry, the number you entered is out of range ');//Please change "number" to the name of the property you want to verify!
}
}
}
}


Phone:/^ ((\d{2,3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7} (\-\d{1,4})? $/
Mobile:/^ ((\d{2,3}\) | ( \d{3}\-))? 13\d{9}$/
URL:/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/
Idcard:/^\d{15} (\d{2}[a-za-z0-9])? $/
QQ:/^[1-9]\d{4,8}$/
Some special amount:/^ ((\d{1,3} (, \d{3}) | ( \d+)) (\.\d{2})? $///Description: Except for "XXX xx,xxx xx,xxx.00" format

Provide the. Trim () property for each of the JS verification methods provided above
String.prototype.trim=function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

Call:
<input type= "text" name= "str" >
<input type= "button" value= "OK" onclick= "" >//onclick Write your own JS validation function to invoke

JS Validation Form JS commit validation class

Add: JS Verify Radio is selected
<script language= "JavaScript" >
function Checkform (obj)
{
for (i=0;i<obj.oo.length;i++)
if (obj.oo[i].checked==true) return true;

Alert ("Please select")
return false;

}
</script>
<form id= "Form1" Name= "Form1" method= "Post" action= "" onsubmit= "return Checkform (This)" >
<input type= "Radio" Name= "oo" value= "RadioButton"/>
<input type= "Radio" Name= "oo" value= "RadioButton"/>
<input type= "Submit" name= "Submit" value= "Submission"/>
</form>

JavaScript-Regular Expressions

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.