js judge the input data length, mobile phone number, email address, and others

Posted by Strikebf on Sun, 20 Feb 2022 08:50:18 +0100

js to judge the number of Chinese characters

//**************************************************************** 
//*Name: DataLength
//*Power: calculate the length of data
//*Entry parameter: fData: data to be calculated
//*The length of the exit is F 2 (the length of the non Unicode parameter is F 2)
//***************************************************************** 

function DataLength(fData) 
{ 
    var intLength=0 
    for (var i=0;i<fData.length;i++) 
    { 
        if ((fData.charCodeAt(i) < 0) || (fData.charCodeAt(i) > 255)) 
            intLength=intLength+2 
        else 
            intLength=intLength+1    
    } 
    return intLength 
} 


//**************************************************************** 
//*Name: IsEmpty
//*Function: judge whether it is empty
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: null
//*False: not null
//***************************************************************** 

function IsEmpty(fData) 
{ 
    return ((fData==null) || (fData.length==0) ) 
} 


//**************************************************************** 
//*Name: IsDigit
//*Function: judge whether it is a number
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: is a number from 0 to 9
//*False: not a number from 0 to 9
//***************************************************************** 

function IsDigit(fData) 
{ 
    return ((fData>="0") && (fData<="9")) 
} 


//**************************************************************** 
//*Name: IsInteger
//*Function: judge whether it is a positive integer
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: it is an integer or the data is empty
//*False: not an integer
//***************************************************************** 

function IsInteger(fData) 
{ 
    //If it is empty, return true
    if (IsEmpty(fData)) 
        return true 
    if ((isNaN(fData)) || (fData.indexOf(".")!=-1) || (fData.indexOf("-")!=-1)) 
        return false    
    
    return true    
} 

//**************************************************************** 
//*Name: IsEmail
//*Function: judge whether it is the correct Email address
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: correct Email address or empty
//*False: wrong Email address
//***************************************************************** 

function IsEmail(fData) 
{ 
    if (IsEmpty(fData)) 
        return true 
    if (fData.indexOf("@")==-1) 
        return false 
    var NameList=fData.split("@"); 
    if (NameList.length!=2) 
        return false  
    if (NameList[0].length<1 ) 
        return false   
    if (NameList[1].indexOf(".")<=0) 
        return false 
    if (fData.indexOf("@")>fData.indexOf(".")) 
return false 
    if (fData.indexOf(".")==fData.length-1) 
return false 
    
    return true    
} 

//**************************************************************** 
//*Name: IsPhone
//*Function: judge whether it is the correct telephone number (including "()", "()", "+", "-" and spaces)
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: correct phone number, or empty
//*False: wrong phone number
//*Error message:
//***************************************************************** 

function IsPhone(fData) 
{ 
    var str; 
    var fDatastr=""; 
    if (IsEmpty(fData)) 
        return true 
    for (var i=0;i<fData.length;i++) 
    { 
        str=fData.substring(i,i+1); 
        if (str!="(" && str!=")" && str!="(" && str!=")" && str!="+" && str!="-" && str!=" ") 
           fDatastr=fDatastr+str; 
    }  
    //alert(fDatastr);  
    if (isNaN(fDatastr)) 
        return false 
    return true    
} 

//**************************************************************** 
//*Name: IsPlusNumeric
//*Function: judge whether it is a correct positive number (including decimal part)
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: correct positive number, or empty
//*False: wrong positive number
//*Error message:
//***************************************************************** 

function IsPlusNumeric(fData) 
{ 
    if (IsEmpty(fData)) 
        return true 
    if ((isNaN(fData)) || (fData.indexOf("-")!=-1)) 
        return false 
    return true    
} 

//**************************************************************** 
//*Name: IsNumeric
//*Function: judge whether it is a correct number (it can be negative or decimal)
//*Entry parameter: fData: data to be checked
//*Exit parameter: True: correct number or empty
//*False: wrong number
//*Error message:
//***************************************************************** 

function IsNumeric(fData) 
{ 
    if (IsEmpty(fData)) 
        return true 
    if (isNaN(fData)) 
        return false 
        
    return true    
} 


//**************************************************************** 
//*Name: IsIntegerInRange
//*Function: judge whether a number is within the specified range
//*Entry parameter: funput: data to be checked
//*fLower: check the lower limit of the range. If there is no lower limit, use null
//*fHigh: check the upper limit of. If there is no upper limit, please use null
//*Exit parameter: True: within the specified range
//*False: out of specified range
//***************************************************************** 

function IsIntegerInRange(fInput,fLower,fHigh) 
{ 
    if (fLower==null) 
        return (fInput<=fHigh) 
    else if (fHigh==null) 
        return (fInput>=fLower) 
    else         
        return ((fInput>=fLower) && (fInput<=fHigh)) 
}

Topics: Javascript