当前位置:网站首页>Form validation and regular expression (2)

Form validation and regular expression (2)

2022-07-23 05:34:00 sl105105

String Classes can support regular methods :
search=》 Returns the subscript of the matching string , Otherwise return to -1
match=》 If no matching text is found , return null. otherwise , It will return an array ( Depends on whether there is a global flag g)
replace=》 Returns a new string
split=》 Returns an array of strings
/*search() Method - Use regular expressions to retrieve abc*/
var str="helloabc";
console.log(str.search(/abc/));  //6
console.log(str.search(/ABC/));  //-1
 console.log(str.search(/ABC/i)); //6
/*match() Method - Use regular expressions to retrieve all numbers in a string */
var str="1 and 2 and 3";
console.log(str.match(/\d/)); //[1]
console.log(str.match(/efg/)); // null
console.log(str.match(/\d/g)); //[1,2,3]
/*replace() Method - Use "abc" Replace in string "yang"*/
var str="hello yang";
console.log(str.replace(/yang/,"abc")); //hello abc
var str="helloyang, How are you?yang";
console.log(str.replace(/yang/g,"abc")); //helloabc, How are you?abc
Replace... With a function
Second use replaceAll()
/*split() Method - Divide words into letters */
console.log("hello".split("")); //h e l l o
console.log("hello".split("",3)); //[h e l]
console.log("h1e2l3l4o".split(/\d/));  //hello
Regular verification
verification 26 A string of English letters
Validation consists of numbers and 26 A string of English letters
Verification by numbers 、 Letter 、 String of underscores
Verify Chinese characters
/* verification 26 A string of English letters */
var str="sgfhdfGIYYjhj";
var reg=/^[a-zA-Z]+$/;   // Once or more "+"
console.log(reg.test(str));
/* Validation consists of numbers and 26 A string of English letters */
var str="abc123DFjh";
var reg=/^[0-9a-zA-Z]+$/;
console.log(reg.test(str));
/* Verification by numbers 、 Letter 、 String of underscores */
var str="abc_123_DFjh";
var reg=/^\w+$/;
console.log(reg.test(str));
/* Verify Chinese characters */
var str=" My motherland ";
var reg=/^[\u4e00-\u9fa5]+$/
console.log(reg.test(str));
practice 1:
Use regular validation to verify the following requirements
A string with spaces at the beginning and end ( Verify and clear the front 、 Back 、 Before and after and all the spaces )
Verify phone number
Verify email
/* Verify the string with spaces at the beginning and end */
        varstr= "    abc   "
        varreg= /^\s+ /   // Go to the front space
        console. log( str. replace( reg, ''));
        // Go to the space behind
        varstr= ' abc  '
        varreg= /\s+ $/
        console. log( str. replace( reg, ''));
        // Go to the space before and after
        varstr= ' abc  '
        varreg= /^\s+ |\s+ $/g
        console. log( str. replace( reg, ''));
        // Clear all spaces
        varstr= ' abc hy  '
        varreg= /\s+ /g
        console. log( str. replace( reg, ''));
        /* Verify phone number */
        varstr= "13502023437";
        varreg= /^1[34578][0-9]{9} $/
       varreg= /^1[34578]\d{9} $/
        console. log( reg. test( str));
        /* Verify email */
        varstr= "[email protected]";
       /*  \ Represents an escape character , hold . Convert to string , And use () grouping , Namely .com perhaps .cn As a group
        varreg= /^[a-zA-Z0-9_-]+ @[a-zA-Z0-9_-]+ (\.[a-zA-Z0-9_-]+ )+ $/
            /*  It can be abbreviated as follows
         var reg =/^[\w-][email protected][\w-]+(\.[\w-]+)+$/
        console. log( reg. test( str));
practice 2:
Use regular to add brackets to the numbers in the following string
var str='abc123efg';  become 'abc[1][2][3]efg'
varstr= 'abc345efg';
        varreg1 = /\d/g;
        vararr = str. split( '');
        for( vari= 0; i< arr. length; i++)
        {
            if( isNaN( arr[ i])== false)
            {
                arr[ i] = '['+ arr[ i]+ ']';
            }
        }
        str2 = arr. join( '');
        alert( str2);
Use isNaN There is one   defects , If there is a space, he will also return fales, So we use regular better , Pictured :
Comprehensive case - Form validation
user.οnblur=function(){
           reg=/^[\w\u4e00-\u9fa5]{6,12}$/;
           if(user.value==''){
            user_p.innerHTML='<i class="err"></i> Can't be empty ';
            return false;
        }
        else if(!reg.test(user.value)){
            user_p.innerHTML='<i class="err"></i> The length is not in the range or there are illegal characters '
            return false;
        }
        // The user name input is consistent with the requirements
        else{
            user_p.innerHTML='<i class="ok"></i> correct '
            return true
        }
}
Form validation Login password requirements
psd.οnblur=function(){
        var reg=/^\w{6,12}$/;
        var reg1=/[^0-9]/;
        var reg2=/[^a-zA-Z]/;
        if(psd.value==''){
            psd_p.innerHTML='<i class="err"></i> Can't be empty ';
            return false;
        }
        // The length is in 6-12 Between Alphanumeric underline
        else if(!reg.test(psd.value)){
            psd_p.innerHTML='<i class="err"></i> The length is not in the range or there are illegal characters '
            return false;
        }
  else if(!reg1.test(psd.value))
        {
            psd_p.innerHTML='<i class="err"></i> It can't be all Numbers '
            return false;
        }
        else if(!reg2.test(psd.value)){
            psd_p.innerHTML='<i class="err"></i> Not all letters '
            return false;
        }
        // The password input is consistent with the requirements
        else{
            psd_p.innerHTML='<i class="ok"></i> correct '
            return true;
        }
Form validation Confirm password requirements
psd2.οnblur=function(){
        if(psd2.value==""){
            psd2_p.innerHTML='<i class="err"></i> Can't be empty ';
            return false;
        }
        // The two passwords are inconsistent
        else if(psd.value!=psd2.value){
            psd2_p.innerHTML='<i class="err"></i> The two passwords don't match ';
            return false;
        }
        // The password input is consistent with the requirements
        else{
            psd2_p.innerHTML='<i class="ok"></i> correct '
            return true;
        }
}
原网站

版权声明
本文为[sl105105]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221753487642.html