Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use regular expressions to validate strings in javascript

Shulou Source: shulou.com Published: 2022-06-02 12:57:10 10月06日 Update

In this issue, the editor will bring you about how to use regular expressions to verify strings in javascript. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Var reg=/ regular expression /

Boolean reg.test (string to verify)

The js engine converts / regular expression / "" into a RegExp object and returns true when the string meets the requirements of the regular expression.

An example of expression verification I wrote: the functions are as follows:

User name, cannot be empty

Password 6 is a number

Password confirmation. The password must be the same two times.

TestTablel.html # d1 {width: 400px; height: 250px; background-color: # FFE4B5; margin: 40px auto;} # d1_head {color: white; font-size: 20px; font-family: "Arial"; height: 24px; background-color: bule;} # d1_content {padding-left: 30px; padding-top: 30px;} .S1 {color: red; font-style: italic;} .s2 {border: 2px dotted blue } function check_username () {var txtObj=document.getElementById ("username"); txtObj.className=''; var msgObj=docuemnt.getElementById ("username_msg"); msgObj [XSS _ clean] =''; if (txtObj.value.length==0) {msgObj [XSS _ clean] = 'user name cannot be empty'; / / assign txtObj.className='s2'; return false;} return true;} function check_pwd () {var pwdObj=document.getElementById ("pwd") to the node's class attribute; pwdObj.classNames='' A test method in var msgObj=document.getElementById ("pwd_msg"); msgObj [XSS _ clean] ='; var reg=/ ^\ d {6} $/; / / javascript returns boolean if (! reg.test (pwdObj.value)) {msgObj [XSS _ clean] = 'password is 6 digits'; pwdObj.className='s2'; return false;} return true;} function check_pwd1 () {var pwdObj=document.getElementById ("pwd"); / / pwdObj.classNames='' / / var msgObj=document.getElementById ("pwd_msg"); / / msgObj1 [XSS _ clean] ='; var pwdObj1=document.getElementById ("pwd1"); / / pwdObj1.classNames=''; var msgObj1=document.getElementById ("pwd_msg1"); msgObj1 [XSS _ clean] =''; if (pwdObj.valuevalueroompwdObj1.value) {msgObj1 [XSS _ clean] = 'password inconsistency, please re-enter'; return false;} return true } function check_form () {var flag=check_username () & & check_pwd () & & check_pwd1; return flag;} Registration password the above is shared by Xiaobian on how to use regular expression verification strings in javascript. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Password expression regular character string authentication user user name content analysis input same consistent professional small and medium digit rich in content function object that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MySQL OPPO Reno MariaDB Redmi