JavaScript judge character length number Email telephone and other common judgment function sharing

  • 2020-05-19 04:15:59
  • OfStack


/****************************************************************
//*  Name: DataLength
//*  work    Can: calculate the length of data 
//*  Entry parameters: fData : data to be calculated 
//*  Exit parameter: return fData The length of the (Unicode Length of 2 , Unicode Length of 1)
//*****************************************************************
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
//*  work    Can: judge whether it is empty 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True Empty:                
//*      False : not empty 
//*****************************************************************
function IsEmpty(fData)
{
  return ((fData==null) || (fData.length==0) )
} 


//****************************************************************
//*  Name: IsDigit
//*  work    Can: judge whether it is a number 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True Is: 0 to 9 The number of                
//*      False : it's not 0 to 9 The number of  
//*****************************************************************
function IsDigit(fData)
{
  return ((fData>="0") && (fData<="9"))
} 


//****************************************************************
//*  Name: IsInteger
//*  work    Can: judge whether it is a positive integer 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True : is an integer, or the data is empty               
//*      False : not an integer 
//*****************************************************************
function IsInteger(fData)
{
  // If null, returns true
  if (IsEmpty(fData))
    return true
  if ((isNaN(fData)) || (fData.indexOf(".")!=-1) || (fData.indexOf("-")!=-1))
    return false  
  
  return true  
} 

//****************************************************************
//*  Name: IsEmail
//*  work    Can: judge whether it is correct Email address 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True : the right Email Address, or empty                
//*      False : the 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
//*  work    Can: determine if the phone number is correct (can include "()" , " (a) " , "+" , "-" And Spaces) 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True : correct phone number, or empty                
//*      False Wrong 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
//*  work    Can: determine whether it is a correct positive number (can include decimal parts) 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: True : correct positive number, or null                
//*      False : false positive number 
//*  Error message: 
//*****************************************************************
function IsPlusNumeric(fData)
{
  if (IsEmpty(fData))
    return true
  if ((isNaN(fData)) || (fData.indexOf("-")!=-1))
    return false 
  return true  
} 

//****************************************************************
//*  Name: IsNumeric
//*  work    Can: judge whether the number is correct (can be negative, decimal) 
//*  Entry parameters: fData : the data to be checked 
//*  Export parameters: 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
//*  work    Judgment can be: 1 Whether the number is within the specified range 
//*  Entry parameters: fInput : the data to be checked 
//*      fLower : check the lower limit of range, if there is no lower limit, please use null
//*      fHigh : check the upper limit, if there is no upper limit, please use null
//*  Export parameters: True : within the specified scope                
//*      False : beyond the 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))
}


Related articles: