I was wondering how I would write a regex expression which says 'and NO whitespaces'.I need to implement this into the following if statement, see below:
$('#postcode').blur(function(){
var postcode = $(this), val = postcode.val();
if((val.length >= 5) && (*******)){
postcode.val(val.slice(0, -3)+' '+val.slice(-3)).css('border','1px solid #a5acb2');
}else{
$(this).css('border','1px solid red');
}
});
Any help is greatly appreciated, Thanks
Try this:
&& (!val.match(/\s/))
match
return null
if there are no spaces (or at least one space), so you can use it as a condition.
&& (val.indexOf(" ") == -1)
注意:如果可以使用其他选项,则不应使用正则表达式。
Would cleaning the whitespaces before line 3 of your code help? (probably less intrusive)
You can do both in the same regexp (guessing postcode is digits).
('1234567').match(/^\d{5,}$/) // ['1234567']
('1234').match(/^\d{5,}$/) // null
('12 34').match(/^\d{5,}$/) //null
('123 4567').match(/^\d{5,}$/) //null
so instead of:
if((val.length >= 5) && (*******)){
//code
}
use:
if(val.match(/^\d{5,}$/)) {
//code
}
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.