Home >Web Front-end >JS Tutorial >Several good form validation functions discovered from Alimama_Form special effects

Several good form validation functions discovered from Alimama_Form special effects

WBOY
WBOYOriginal
2016-05-16 19:08:561348browse

//--------
// Check if 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 form (only applicable to IE browser)
//--------
function formSaveCheck(fileName){
if(isNetscape() ){alert("Sorry, these function is not supported")}
else document.execCommand('SaveAs',null,fileName)
}

//------ -
// Verify the validity of the data
//--------
function isValidReg( chars){
var re=/|[|]|{ |}|『|』|※|○|●|◎|§|△|▲|☆|★|◇|◆|□|▼|㊣|﹋|⊕|⊙|〒|ㄅ|ㄆ|
ㄇ|ㄈ|ㄉ|ㄊ|ㄋ|ㄌ|ㄍ|ㄎ|ㄏ|ㄐ|ㄑ|ㄒ|ㄓ|ㄔ|ㄕ|ㄖ|ㄗ|ㄘ|ㄙ|ㄚ|ㄛ|ㄜ|ㄝ|ㄞ|ㄟ| ㄢ|ㄣ|ㄤ|ㄥ|ㄦ|ㄧ|ㄨ|ㄩ
|■|▄|▆|*|@|#|^|\/;
if (re.test( chars) == true ) {
return false;
}else{
return true;
} }
}

//--------
// Check Is the length of the data legal?
//--------
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; se;
}

//--------
// Check the validity of the number
//--------
function isValidDecimal( chars ) {
var re=/^d*.?d{1,2}$/;
if (chars.match(re) == null)
return false;
else
return true;
}

//--------
// Check 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 postal code
//--------
function isValidPost( chars ) {
var re=/^d{6}$/;
if (chars.match(re) == null)
return false;
else
return true;
}

//--------
// Remove leading and trailing null characters from the data
//--------
function jsTrim(value){
return value.replace(/(^s*)|(s*$)/g,"");
}

//--------
// Verify whether the data is empty (it is also NULL when the data is a null character)
//------- -
function isNULL(chars) {
if (chars == null)
return true;
if (jsTrim(chars).length==0)
return true;
return false;
}

//--------
// Verify 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 {
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 a
    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].length3) 
    {
        // 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    {
        if (bMsg) alert("This address is missing a hostname!")
        return false
    }

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

//--------
// 判断是否为闰年
//--------
function isLeapYear(year){
  if (year % 4 != 0)
    return false;
  if (year % 400 == 0)
    return true;
  if (year % 100 == 0)
    return false;
  return true;
}

//--------
// 校验日期的合法性
//--------
function validateDate(day,month,year)
{
    if ((day        return false;

    if ((month>=1)&&(month        if (month == 2) {
            if (isLeapYear(year)) {
                if (day                    return true;
            } else {
                if (day                    return true;
                else
                    return false;
            }
        } else if ((month==4)||(month==6)||(month==9)||(month==11)) {
            if (day                return true;
            else
                return false;
        } else {
            if (day                return true;
            else
                return false;
        }
}

return false;
}

//--------
// Determining whether the data contains is 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
// Parameters: url - The pop-up window displays the content of the URL
//                                                                                                                                                         Height
// isCenter-controls whether the pop-up window is displayed in the center of the screen, the value is true/false
// isResizable-controls whether the pop-up window can be resized, the value is true/false
// isScroll-control Whether the pop-up window has scroll bars, 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
// Parameters: url-The content of the URL displayed in the pop-up window
//               w-                                                                                                                                                                  🎜>// isCenter-Control whether the pop-up window is displayed in the center of the screen, the value is true/false
// isResizable-Control whether the pop-up window can change the size, the value is true/false
// isModal-Control the pop-up window Whether it is a modal or non-modal dialog box, 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');
}

//---------
// 팝업창
// 매개변수: url - 팝업창에 표시되는 URL 내용
/ /                                                                                                                              팝업 창 🎜>// h-팝업 창의 높이
// isCenter-팝업 창을 화면 중앙에 표시할지 여부를 제어하며 값은 true/false
// IsRessible-팝업 창의 크기를 조정할 수 있는지 여부를 제어합니다. 값은 true/false입니다.
//   isScroll-팝업 창에 스크롤 막대가 있는지 여부를 제어합니다. 값은 true/false입니다.
/ /---------
function openWindowCenter(url,w,h){
var top=(window.screen.height-h)/2
var left=(window.screen) .width-w)/2;
var param='toolbar=no,menubar=no,scrollbars= yes,resizing=no,location=no, status=no,top='
param=param top ;
param=param ',left=';
param =param ',height=' h
param=param ',width=' w; var w=window.open (url,"",param)
if(w!=null && typeof (w)!="undefine"){
w.focus()
}
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn