Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use regular expressions to validate strings in javascript

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report