Several good form verification functions found by alimama _ form special effects

Source: Internet
Author: User
Several good form verification functions found by alimama //--------
// Check whether the current browser is Netscape
//--------
Function isNetscape (){
App = navigator. appName. substring (0, 1 );
If (app = 'n') return true;
Else {return false ;}
}

//--------
// Save the current Form (only applicable to IE)
//--------
Function formSaveCheck (fileName ){
If (isNetscape () {alert ("Sorry, these function is not supported ")}
Else document.exe cCommand ('saveas', null, fileName)
}

//--------
// Check the validity of the data
//--------
Function isValidReg (chars ){
Var re =/<|> | \ [| \] | \ {| \} | '| ※| ○ | ● | ◎ | § | △| ▲| ☆|★| ◇ | ◆ | □| Medium | ⊙ | medium |
California | Connecticut | california | Connecticut |
| ■ | Channels | \ * | @ | # | \ ^ | \\/;
If (re. test (chars) = true ){
Return false;
} Else {
Return true;
}
}

//--------
// Check whether the data length is valid
//--------
Function isValidLength (chars, len ){
If (chars. length> len ){
Return false;
}
Return true;
}

//--------
// Verify the validity of the URL
//--------
Function isValidURL (chars ){
// Var re =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) (\ w + (-* \ w *) + )\.) + (com) |
(Net) | (edu) | (gov) | (org) | (biz) | (aero) | (coop) | (info) | (name) | (pro) | (museum ))(\. ([a-z] {2 }))?) | (\ W + (-
* \ W *) +) \. (cn) $ /;
Var re =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) (\ S + \. \ S +) $ /;
// Var re =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) (\ w + (-* \ w *) + )\.) + (com) |
(Net) | (edu) | (gov) | (org) | (biz) | (aero) | (coop) | (info) | (name) | (pro) | (museum) | (cn) | (TV) | (hk ))(\. ([a-z]
{2 }))?) | (\ W + (-* \ w *) +) \. (cn) (\/| \?) \ S *) $ /;
If (! IsNULL (chars )){
Chars = jsTrim (chars );
If (chars. match (re) = null)
Return false;
Else
Return true;
}
Return false;
}

//--------
// Verify the validity of the number
//--------
Function isValidDecimal (chars ){
Var re =/^ \ d *\.? \ D {1, 2} $ /;
If (chars. match (re) = null)
Return false;
Else
Return true;
}

//--------
// Verify the validity of the number
//--------
Function isNumber (chars ){
Var re =/^ \ d * $ /;
If (chars. match (re) = null)
Return false;
Else
Return true;
}

//--------
// Verify the validity of the zip code
//--------
Function isValidPost (chars ){
Var re =/^ \ d {6} $ /;
If (chars. match (re) = null)
Return false;
Else
Return true;
}

//--------
// Remove the first and last null characters of the data
//--------
Function jsTrim (value ){
Return value. replace (/(^ \ s *) | (\ s * $)/g ,"");
}

//--------
// Check whether the data is empty (if the data is empty, it is also NULL)
//--------
Function isNULL (chars ){
If (chars = null)
Return true;
If (jsTrim (chars). length = 0)
Return true;
Return false;
}

//--------
// Check the validity of the Email
//--------
Function checkEmail (fieldName, bMsg)
{
Var emailStr = fieldName. value;

Var emailPat =/^ (. +) @ (. +) $/
Var specialChars = "\\ (\\) <>@,;:\\\\\\" \\. \ [\\]"
Var validChars = "\ [^ \ s" + specialChars + "\]"
Var quotedUser = "(\" [^ \ "] * \")"
Var ipDomainPat =/^ \ [(\ d {1, 3 })\. (\ d {1, 3 })\. (\ d {1, 3 })\. (\ d {1, 3}) \] $/
Var atom = validChars +'
Var word = "(" + atom + "|" + quotedUser + ")"
Var userPat = new RegExp ("^" + word + "(\." + word + ") * $ ")
Var domainPat = new RegExp ("^" + atom + "(\." + atom + ") * $ ")

Var matchArray = emailStr. match (emailPat)
If (matchArray = null)
{
If (bMsg) alert ("Email address seems incorrect (check @ and.'s )")
Return false
}
Var user = matchArray [1]
Var domain = matchArray [2]

// See if "user" is valid
If (user. match (userPat) = null)
{
If (bMsg) alert ("The Email address seems incorrect .")
// FieldName. focus ();
Return false
}

/* If the e-mail address is at an IP address (as opposed to a symbolic
Host name) make sure the IP address is valid .*/
Var IPArray = domain. match (ipDomainPat)
If (IPArray! = Null)
{
For (var I = 1; I <= 4; I ++)
{
If (IPArray [I]> 255)
{
If (bMsg) alert ("Destination IP address is invalid! ")
Return false
}
}
Return true
}

// Domain is symbolic name
Var domainArray = domain. match (domainPat)
If (domainArray = null)
{
If (bMsg) alert ("The domain name doesn't seem to be valid .")
Return false
}

/* Domain name seems valid, but now make sure that it ends in
Three-letter word (like com, edu, gov) or a two-letter word,
Representing country (uk, nl), and that there's a hostname preceding
The domain or country .*/

Var atomPat = new RegExp (atom, "g ")
Var domArr = domain. match (atomPat)
Var len = domArr. length
If (domArr [domArr. length-1]. length <2 | domArr [domArr. length-1]. length> 3)
{
// The address must end in a two letter or three letter word.
If (bMsg) alert ("The address must end in a three-letter domain, or two letter country .")
Return false
}

// Make sure there's a host name preceding the domain.
If (len <2)
{
If (bMsg) alert ("This address is missing a hostname! ")
Return false
}

// If we 've got this far, everything's valid!
Return true;
}

//--------
// Determine whether it is a leap year
//--------
Function isLeapYear (year ){
If (year % 4! = 0)
Return false;
If (year % 400 = 0)
Return true;
If (year % 100 = 0)
Return false;
Return true;
}

//--------
// Check validity of the date
//--------
Function validateDate (day, month, year)
{
If (day <= 0) | (month <= 0) | (year <= 0 ))
Return false;

If (month> = 1) & (month <= 12 )){
If (month = 2 ){
If (isLeapYear (year )){
If (day <= 29)
Return true;
} Else {
If (day <= 28)
Return true;
Else
Return false;
}
} Else if (month = 4) | (month = 6) | (month = 9) | (month = 11 )){
If (day <= 30)
Return true;
Else
Return false;
} Else {
If (day <= 31)
Return true;
Else
Return false;
}
}

Return false;
}

//--------
// Determine whether all data is contained in a Single Byte
//--------
Function isSingleByteString (str)
{
Var rc = true;
Var j = 0, I = 0;
For (I = 0; I J = str. charCodeAt (I );
If (j> = 128 ){
Rc = false;
Break;
}
}
Return rc;
}

Var submitEvent = true;
Function checkDoubleSubmit (){
Return submitEvent;
}

//--------
// Pop-up window
// Parameter: url-the URL content is displayed in the pop-up window.
// W-width of the pop-up window
// H-height of the pop-up window
// IsCenter-determines whether the pop-up window is displayed in the center of the screen. The value is true/false.
// IsResizable-controls whether the size of the pop-up window can be changed. The value is true/false.
// IsScroll-controls whether a scroll bar exists in the pop-up window. The value is true/false.
//--------
Function popupWindow (url, w, h, isCenter, isResizable, isScroll ){
If (isNULL (url) return;
Var scrLeft = 0;
Var scrTop = 0;
Var scroll = "no ";
Var resize = "no ";
If (isCenter ){
ScrLeft = (screen. width-w)/2;
ScrTop = (screen. height-h)/2;
}
If (isResizable) resize = "yes ";
If (isScroll) scroll = "yes ";
Window. open (url, 'popupwindow ',
'Height = '+ h +', width = '+ w +', top = '+ scrTop +', left = '+ scrLeft +', toolbar = no, menubar = no, scrollbars = '+ scrol
L + ', resizable =' + resize + ', location = no, status = no ');
}

//--------
// Pop-up window
// Parameter: url-the URL content is displayed in the pop-up window.
// W-width of the pop-up window
// H-height of the pop-up window
// IsCenter-determines whether the pop-up window is displayed in the center of the screen. The value is true/false.
// IsResizable-controls whether the size of the pop-up window can be changed. The value is true/false.
// IsModal-control whether the pop-up window is in mode or not. The value is true/false.
//--------
Function popupModalWindow (url, w, h, isCenter, isResizable, isModal ){
If (isNULL (url) return;
Var scrLeft = 0;
Var scrTop = 0;
Var resize = "no ";
Var cnt = "no ";
If (isCenter ){
Cnt = "yes ";
ScrLeft = (screen. width-w)/2;
ScrTop = (screen. height-h)/2;
}
If (isResizable) resize = "yes ";
If (isModal)
Window. showModalDialog (url, 'popupwindow ',
'Dialogwidth: '+ w + 'px; dialogHeight:' + h + 'px; dialogLeft: '+ scrLeft + 'px; dialogTop:' + scrTop + 'px; center :'
+ Cnt + '; help: no; resizable:' + resize + '; status: no ');
Else
Window. showModelessDialog (url, 'popupwindow ',
'Dialogwidth: '+ w + 'px; dialogHeight:' + h + 'px; dialogLeft: '+ scrLeft + 'px; dialogTop:' + scrTop + 'px; center :'
+ Cnt + '; help: no; resizable:' + resize + '; status: no ');
}

//--------
// Pop-up window
// Parameter: url-the URL content is displayed in the pop-up window.
// W-width of the pop-up window
// H-height of the pop-up window
// IsCenter-determines whether the pop-up window is displayed in the center of the screen. The value is true/false.
// IsResizable-controls whether the size of the pop-up window can be changed. The value is true/false.
// IsScroll-controls whether a scroll bar exists in the pop-up window. The value is true/false.
//--------
Function openWindowCenter (urll, w, h ){
Var top = (window. screen. height-h)/2;
Var left = (window. screen. width-w)/2;
Var param = 'toolbar = no, menubar = no, scrollbars = yes, resizable = no, location = no, status = no, top = ';
Param = param + top;
Param = param + ', left = ';
Param = param + left;
Param = param + ', height =' + h;
Param = param + ', width =' + w;
Var w = window. open (urll, "", param)
If (w! = Null & typeof (w )! = "Undefined "){
W. focus ();
}
}

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.