Js USES regular expressions to validate the complete resources of the form of

  • 2020-03-29 23:49:32
  • OfStack

Take a look at regular expressions while studying form validation in your web page

Found a relatively complete resource on the Internet, a little bit of typesetting

 
//Check for a valid real name, which can only contain Chinese or capitalized English letters
function isValidTrueName(strName){ 
var str = Trim(strName); 
//Determines whether it is all English capitalized or all Chinese, and can include Spaces
var reg = /^[A-Z u4E00-u9FA5]+$/; 
if(reg.test(str)){ 
return false; 
} 
return true; 
} 

JavaScript forms validate age
The JavaScript form validates age to determine whether an input is age-appropriate, using regular expressions.
 
//Check the age

function isAge(str){ 
var mydate=new Date; 
var now=mydate.getFullYear(); 
if (str < now-60 || str > now-18){ 
return false; 
} 
return true; 
} 

The JavaScript form validates the phone number
The JavaScript form validates a phone number to determine whether an input quantity is a phone number, using regular expressions.
 
<SPAN style="FONT-SIZE: 12px">//Check the phone number
</SPAN><SPAN style="FONT-SIZE: 10px">function isTel(str){ 
var reg=/^([0-9]|[-])+$/g ; 
if(str.length18){ 
return false; 
} 
else{ 
return reg.exec(str); 
} 
}</SPAN> 

Regular expressions validate mailboxes
The JavaScript form validates email to determine if an input is E-mail, using regular expressions.
 
//Check email

function isEmail(str){ 

var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/; 

return reg.test(str); 

} 

JavaScript forms validate Chinese capital letters
JavaScript form validates Chinese capital letters to determine whether an input is Chinese or capital English letters, through regular expressions.
 
//Check for a valid real name, which can only contain Chinese or capitalized English letters

function isValidTrueName(strName){ 
var str = Trim(strName); 
//Determines whether it is all English capitalized or all Chinese, and can include Spaces
var reg = /^[A-Z u4E00-u9FA5]+$/; 
if(reg.test(str)){ 
return false; 
} 
return true; 
} 

JavaScript validation
JavaScript forms validate age
The JavaScript form validates age to determine whether an input is age-appropriate, using regular expressions.
 
//Check the age

function isAge(str){ 

var mydate=new Date; 

var now=mydate.getFullYear(); 

if (str < now-60 || str > now-18){ 
return false; 
} 
return true; 
} 

 For the function of time and date, please refer to the JavaScript In the getDate Date function  

JavaScript forms validate Chinese capital letters
JavaScript form validates Chinese capital letters to determine whether an input is Chinese or capital English letters, through regular expressions.
 
//Check for a valid real name, which can only contain Chinese or capitalized English letters

function isValidTrueName(strName){ 

var str = Trim(strName); 

//Determines whether it is all English capitalized or all Chinese, and can include Spaces

var reg = /^[A-Z u4E00-u9FA5]+$/; 

if(reg.test(str)){ 

return false; 

} 

return true; 

} 

The JavaScript form verifies that it is Chinese
The JavaScript form validates whether an input is in Chinese, and determines whether an input is in Chinese, using regular expressions
 
//Check if it is in Chinese
function isChn(str){ 
var reg = /^+$/; 
if(!reg.test(str)){ 
return false; 
} 
return true; 
} 

The JavaScript form validates the password
JavaScript form authentication password is to check whether the input field is a valid password,
Passwords are only allowed to consist of ASCII,
This function is used only when changing or registering a password.
That is, any string that is not made up of ASCII cannot pass validation.
See the following code to demonstrate the checkValidPasswd function
 
function checkValidPasswd(str){ 
var reg = /^[x00-x7f]+$/; 
if (! reg.test(str)){ 
return false; 
} 
if (str.length < 6 || str.length > 16){ 
return false; 
} 
return true; 
} 

JavaScript regular authentication IP
 
JavaScript The regular verification IP 
JavaScript The regular verification IP Purpose: calibration ip Format of address  
 Input: strIP : ip address  
 Return: if JavaScript Through the verification IP return true, Otherwise returns false ;  

JavaScript validation IP The following code  

function isIP(strIP) { 
if (isNull(strIP)) return false; 
var re=/^(d+).(d+).(d+).(d+)$/g //A regular expression that matches an IP address
if(re.test(strIP)) 
{ 
if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true; 
} 
return false; 
} 

 To determine whether the obtained is or is not through regularization IP The format of the address and return the corresponding result  


Related articles: