Regular expressions in JavaScript in several instances _ regular expressions

Source: Internet
Author: User
Tags port number
! Remove the processing of whitespace on both ends of a string

If you take the traditional approach, you might want to take the following approach.
Clear left Space
function Js_ltrim (DESTSTR)
{
if (deststr==null) return "";
var pos=0;
var retstr=new String (DESTSTR);
if (retstr.lenght==0) return retstr;
while (retstr.substring (pos,pos+1) = = "") pos++;
Retstr=retstr.substring (POS);
return (RETSTR);
}
Clear right space
function Js_rtrim (DESTSTR)
{
if (deststr==null) return "";
var retstr=new String (DESTSTR);
var pos=retstr.length;
if (pos==0) return retstr;
while (POS && retstr.substring (pos-1,pos) = "") pos--;
Retstr=retstr.substring (0,pos);
return (RETSTR);
}
Clear left and right spaces
function Js_trim (DESTSTR)
{
if (deststr==null) return "";
var retstr=new String (DESTSTR);
var pos=retstr.length;
if (pos==0) return retstr;
Retstr=js_ltrim (RETSTR);
Retstr=js_rtrim (RETSTR);
return retstr;
}

Using regular expressions, to remove the spaces on either side, just the following code
String.prototype.trim = function ()
{
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}

One sentence is done,
The visible regular expression saves us a considerable amount of written code


! Check of mobile phone number

If you are using a traditional calibration method, you must complete the following three steps.
(1). Whether it is a number
(2). is 11-bit
(3). Whether the third digit is 5,6,7,8,9
If you use regular expression checksums, just the following code
function CheckMobile1 (form)
{
if (Form.mobile.value > "")
{
var reg=/13[5,6,7,8,9]\d{8}/;
if (Form.mobile.value.match (reg) = null)
{
Alert ("Please enter the correct mobile phone number!") ");
Form.mobile.focus (); return false;
}
}
return true;
}

From the above code can be seen verified mobile phone number only need to define a var reg=/13[5,6,7,8,9]\d{8}/, pattern matching string can complete the validity of the check

! The checksum of the URL,
Condition: Must begin with http://or https://, the port number must be between 1-65535, the following code completes the validation of the legality

OBJ: Data Objects
DISPSTR: Failure prompt Content Display string
function Checkurlvalid (obj, dispstr)
{
if (obj = null)
{
Alert ("Incoming object is null");
return false;
}
var str = obj.value;

var urlpatern0 =/^https?:\ /\/.+$/i;
if (!urlpatern0.test (str))
{
Alert (dispstr+ "illegal: Must start with ' http:\/\/' or ' https:\/\/')";
Obj.focus ();
return false;
}

var urlpatern2=/^https?:\ /\/([a-za-z0-9_-]) + (\.)?) * (: \d+)? +$/i;
if (!urlpatern2.test (str))
{
Alert (dispstr+ "port number must be a number and should be between 1-65535!");
Obj.focus ();
return false;
}


var urlpatern1 =/^https?:\ /\/([a-za-z0-9_-]) + (\.)?) * (: \d+)? (\/((\.)? (\?)? =?&? [a-za-z0-9_-] (\?)?) *) *$/i;

if (!urlpatern1.test (str))
{
Alert (dispstr+ "illegal, please check!");
Obj.focus ();
return false;
}

var s = "0";
var t = 0;
var re = new RegExp (": \\d+", "IG");
while ((arr = re.exec (str))!=null)
{
s = str.substring (Regexp.index+1,regexp.lastindex);

if (s.substring (0,1) = = "0")
{
Alert (dispstr+ "port number cannot begin with 0!");
Obj.focus ();
return false;
}

T = parseint (s);
if (t<1 | | t >65535)
{
Alert (dispstr+ "port number must be a number and should be between 1-65535!");
Obj.focus ();
return false;
}
}
return true;
}

To the URL of the checksum, it appears that there are a lot of code, this is because to give error prompts, otherwise just var urlpatern1 =/^https?:\ /\/([a-za-z0-9_-]) + (\.)?) * (: \d+)? (\/((\.)? (\?)? =?&? [a-za-z0-9_-] (\?)?) *) *$/i; You can verify that the URL is valid.


JavaScript Regular expression Test
/*********************************************************************************
* Eo_jslib.js
* JavaScript Regular expression test
**********************************************************************************/

The checksum is all made up of numbers
function IsDigit (s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec (s)) return false
return True
}

Verify Login Name: Can only enter 5-20 letters beginning with a letter, can be with numbers, "_", "." The string
function Isregisterusername (s)
{
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
if (!patrn.exec (s)) return false
return True
}

Verify user name: You can enter only 1-30 strings that start with a letter
function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}

Verify password: Can only enter 6-20 letters, numbers, underscores
function ispasswd (s)
{
var patrn=/^ (\w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}

Check ordinary telephone, fax number: You can "+" start, in addition to numbers, can contain "-"
function Istel (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? (\d) {1,12}) +$/;
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Check mobile phone Number: Must start with a number, in addition to the number, can contain "-"
function Ismobil (s)
{
var patrn=/^[+]{0,1} (\d) {1,3}[]? ([-]? ((\d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Check ZIP code
function Ispostalcode (s)
{
var patrn=/^[a-za-z0-9]{3,12}$/;
var patrn=/^[a-za-z0-9]{3,12}$/;
if (!patrn.exec (s)) return false
return True
}

Verify Search Keywords
function Issearch (s)
{
var patrn=/^[^ ' ~!@#$%^&* () +=|\\\][\]\{\}:; ' \,.<>/?] {1} [^ ' ~!@$%^& () +=|\\\] [\]\{\}:;' \,.<>?] {0,19}$/;
if (!patrn.exec (s)) return false
return True
}

function IsIP (s)//by zergling
{
var patrn=/^[0-9.] {1,20}$/;
if (!patrn.exec (s)) return false
return True
}

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.