简体   繁体   中英

validation of e-mail

How can I do the opposite of the following code. I need to check if it's false at the first place not if it's right.

var a = $("#phone").val();
var filter = /^[0-9]{8}$/;

if(filter.test(a)) {

} else {

}

change

if(filter.test(a))

to

if(!filter.test(a))

the ! will negate the bool

Just put ! before filter.test(a) or use filter.test(a) == false

var a = $("#phone").val();
var filter = /^[0-9]{8}$/;

if(!filter.test(a)) { // or if(filter.test(a) == false)

} else {

}

You already have one way using the if statement as it stands if you need to run 2 versions of code

if(filter.test(a)) {
  /* run code if true*/
} else {
  /* run code if false*/
} 

To reverse the if test simply add exclamation mark

if( !filter.test(a)) {
 /* run code if false*/
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM