开发者

How do I write a regex expression saying 'and NO whitespaces'?

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)+' '+开发者_如何学Pythonval.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)

Note: Regex shouldn't be used if other options are readily available.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜