Common JS verification functions

Source: Internet
Author: User
Tags date1 valid email address

// Check whether it is composed of digits
Function isDigit (s)
{
Var patrn =/^ [0-9] {1, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Check Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".
Function isRegisterUserName (s)
{
Var patrn =/^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Check user name: only 1-30 strings starting with letters can be entered
Function isTrueName (s)
{
Var patrn =/^ [a-zA-Z] {1, 30} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Password verification: only 6-20 letters, numbers, and underscores can be entered
Function isPasswd (s)
{
Var patrn =/^ (\ w) {6, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the phone number and fax number. The phone number can start with "+" and contain "-" in addition to numbers.
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.exe c (s) return false
Return true
}
// Verify the mobile phone number. It must start with a number and can contain "-" in addition to a number.
Function isMobil (s)
{
Var patrn =/^ [+] {0, 1} (\ d) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the zip code
Function isPostalCode (s)
{
// Var patrn =/^ [a-zA-Z0-9] {3, 12} $ /;
Var patrn =/^ [a-zA-Z0-9] {} $ /;
If (! Patrn.exe c (s) return false
Return true
}
// Verify the search keyword
Function isSearch (s)
{
Var patrn =/^ [^ '~! @ # $ % ^ & * () + = |\\] [\] \{\}:;\' \,. <>/?] {1} [^ '~! @ $ % ^ & () + =|\\] [\] \{\}:;\' \,. <>?] {0, 19} $ /;
If (! Patrn.exe c (s) return false
Return true
}
Function isIP (s) // by zergling
{
Var patrn =/^ [0-9.] {1, 20} $ /;
If (! Patrn.exe c (s) return false
Return true
}
/*************************************** **************************************** **
* FUNCTION: isBetween
* PARAMETERS: val AS any value
* Lo AS Lower limit to check
* Hi AS Higher limit to check
* Cballs: NOTHING
* RETURNS: TRUE if val is between lo and hi both transaction Sive, otherwise false.
***************
**************************************** **************************/
Function isBetween (val, lo, hi ){
If (val <lo) | (val> hi) {return (false );}
Else {return (true );}
}
/*************************************** **************************************** **
* FUNCTION: isDate checks a valid date
* PARAMETERS: theStr AS String
* Cballs: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**************************************** **************************************** **/
Function isDate (theStr ){
Var the1st = theStr. indexOf ('-');
Var the2nd = theStr. lastIndexOf ('-');
If (the1st = the2nd) {return (false );}
Else {
Var y = theStr. substring (0, the1st );
Var m = theStr. substring (the1st + 1, the2nd );
Var d = theStr. substring (the2nd + 1, theStr. length );
Var maxDays = 31;
If (isInt (m) = false | isInt (d) = false | isInt (y) = false ){
Return (false );}
Else if (y. length <4) {return (false );}
Else if (! IsBetween (m, 1, 12) {return (false );}
Else if (m = 4 | m = 6 | m = 9 | m = 11) maxDays = 30;
Else if (m = 2 ){
If (y % 4> 0) maxDays = 28;
Else if (y % 100 = 0 & y % 400> 0) maxDays = 28;
Else maxDays = 29;
}
If (isBetween (d, 1, maxDays) = false) {return (false );}
Else {return (true );}
}
}
/*************************************** **************************************** **
* FUNCTION: isEuDate checks a valid date in British format
* PARAMETERS: theStr AS String
* Cballs: isBetween, isInt
* RETURNS: TRUE if theStr is a valid date otherwise false.
**************************************** **************************************** **/
Function isEuDate (theStr ){
If (isBetween (theStr. length, 8, 10) = false) {return (false );}
Else {
Var the1st = theStr. indexOf ('/');
Var the2nd = theStr. lastIndexOf ('/');
If (the1st = the2nd) {return (false );}
Else {
Var m = theStr. substring (the1st + 1, the2nd );
Var d = theStr. substring (0, the1st );
Var y = theStr. substring (the2nd + 1, theStr. length );
Var maxDays = 31;
If (isInt (m) = false | isInt (d) = false | isInt (y) = false ){
Return (false );}
Else if (y. length <4) {return (false );}
Else if (isBetween (m, 1, 12) = false) {return (false );}
Else if (m = 4 | m = 6 | m = 9 | m = 11) maxDays = 30;
Else if (m = 2 ){
If (y % 4> 0) maxDays = 28;
Else if (y % 100 = 0 & y % 400> 0) maxDays = 28;
Else maxDays = 29;
}
If (isBetween (d, 1, maxDays) = false) {return (false );}
Else {return (true );}
}
}
}
/*************************************** **************************************** *
* FUNCTION: Compare Date! Which is the latest!
* PARAMETERS: lessDate, moreDate AS String
* Cballs: isDate, isBetween
* RETURNS: TRUE if lessDate <moreDate
**************************************** **************************************** */
Function isComdate (lessDate, moreDate)
{
If (! IsDate (lessDate) {return (false );}
If (! IsDate (moreDate) {return (false );}
Var less1st = lessDate. indexOf ('-');
Var less2nd = lessDate. lastIndexOf ('-');
Var more1st = moreDate. indexOf ('-');
Var more2nd = moreDate. lastIndexOf ('-');
Var lessy = lessDate. substrin (0, less1st );
Var lessm = lessDate. substring (less1st + 1, less2nd );
Var lessd = lessDate. substring (less2nd + 1, lessDate. length );
Var morey = moreDate. substring (0, more1st );
Var morem = moreDate. substring (more1st + 1, more2nd );
Var mored = moreDate. substring (more2nd + 1, moreDate. length );
Var Date1 = new Date (lessy, lessm, lessd );
Var Date2 = new Date (morey, morem, mored );
If (Date1> Date2) {return (false );}
Return (true );
}
/*************************************** **************************************** **
* FUNCTION isEmpty checks if the parameter is empty or null
* PARAMETER str AS String
**************************************** **************************************** **/
Function isEmpty (str ){
If (str = null) | (str. length = 0) return true;
Else return (false );
}
/*************************************** **************************************** **
* FUNCTION: isInt
* PARAMETER: theStr AS String
* RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE
* Cballs: isDigit
**************************************** **************************************** **/
Function isInt (theStr ){
Var flag = true;
If (isEmpty (theStr) {flag = false ;}
Else
{For (var I = 0; I <theStr. length; I ++ ){
If (isDigit (theStr. substring (I, I + 1) = false ){
Flag = false; break;
}
}
}
Return (flag );
}
/*************************************** **************************************** **
* FUNCTION: isReal
* PARAMETER: heStr AS String
DecLen AS Integer (how many digits after period)
* RETURNS: TRUE if theStr is a float, otherwise FALSE
* Cballs: isInt
**************************************** **************************************** **/
Function isReal (theStr, decLen ){
Var dot1st = theStr. indexOf ('.');
Var dot2nd = theStr. lastIndexOf ('.');
Var K = true;
If (isEmpty (theStr) return false;
If (dot1st =-1 ){
If (! IsInt (theStr) return (false );
Else return (true );
}
Else if (dot1st! = Dot2nd) return (false );
Else if (dot1st = 0) return (false );
Else {
Var intPart = theStr. substring (0, dot1st );
Var decPart = theStr. substring (dot2nd + 1 );
If (decPart. length> decLen) return (false );
Else if (! IsInt (intPart) |! IsInt (decPart) return (false );
Else if (isEmpty (decPart) return (false );
Else return (true );
}
}
/*************************************** **************************************** **
* FUNCTION: isEmail
* PARAMETER: String (Email Address)
* RETURNS: TRUE if the String is a valid Email address
* FALSE if the passed string is not a valid Email Address
* Email format: AnyName @ EmailServer e. g; webmaster@hotmail.com
* @ Sign can appear only once in the email address.
**************************************** **************************************** */
Function isEmail (theStr ){
Var atIndex = theStr. indexOf ('@');
Var dotIndex = theStr. indexOf ('.', atIndex );
Var flag = true;
TheSub = theStr. substring (0, dotIndex + 1)
If (atIndex <1) | (atIndex! = TheStr. lastIndexOf ('@') | (dotIndex <atIndex + 2) | (theStr. length <= theSub. length ))
{Return (false );}
Else {return (true );}
}
/*************************************** **************************************** **
* FUNCTION: newWindow
* PARAMETERS: doc-> Document to open in the new window
Hite-> Height of the new window
Wide-> Width of the new window
Bars-> 1-Scrll bars = YES 0-Scroll Bars = NO
Resize-> 1-Resizable = YES 0-Resizable = NO
* Cballs: NONE
* RETURNS: New window instance
**************************************** **************************************** **/
Function newWindow (doc, hite, wide, bars, resize ){
Var winNew = "_ blank ";
Var pt = "toolbar = 0, location = 0, directories = 0, status = 0, menubar = 0 ,";
Opt + = ("scrollbars =" + bars + ",");
Opt + = ("resizable =" + resize + ",");
Opt + = ("width =" + wide + ",");
Opt + = ("height =" + hite );
WinHandle = window. open (doc, winNew, opt );
Return;
}
/*************************************** **************************************** **
* FUNCTION: DecimalFormat
* PARAMETERS: paramValue-> Field value
* Cballs: NONE
* RETURNS: Formated string
**************************************** **************************************** **/
Function DecimalFormat (paramValue ){
Var intPart = parseInt (paramValue );
Var decPart = parseFloat (paramValue)-intPart;
Str = "";
If (decPart = 0) | (decPart = null) str + = (intPart + ". 00 ");
Else str + = (intPart + decPart );
Return (str );
}
"^ \ D + $" // non-negative integer (positive integer + 0)
"^ [0-9] * [1-9] [0-9] * $" // positive integer
"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)
"^-[0-9] * [1-9] [0-9] * $" // negative integer
"^ -? \ D + $ "// integer
"^ \ D + (\. \ d + )? $ "// Non-negative floating point number (Positive floating point number + 0)
"^ ([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// Positive floating point number
"^ (-\ D + (\. \ d + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)
"^ (-([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $ "// negative floating point number
"^ (-? \ D +) (\. \ d + )? $ "// Floating point number
"^ [A-Za-z] + $" // A string consisting of 26 English letters
"^ [A-Z] + $" // a string consisting of 26 uppercase letters
"^ [A-z] + $" // a string consisting of 26 lowercase letters
"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters
"^ \ W + $" // a string consisting of a number, 26 English letters, or underscores
"^ [\ W-] + (\\. [\ w-] +) * @ [\ w-] + (\\. [\ w-] +) + $ "// email address
"^ [A-zA-z] +: // (\ w + (-\ w + )*)(\\. (\ w + (-\ w + )*))*(\\? \ S *)? $ "// Url

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.