How to write a regex expression labeled "And without spaces"?

I was wondering how I would write a regex expression that would say "and NO whitespaces". I need to implement this in 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 much appreciated, thanks

+3
source share
4 answers

Try the following:

&& (!val.match(/\s/))

matchreturn nullif there are no spaces (or at least one space), so you can use it as a condition.

+4
source
&& (val.indexOf(" ") == -1)

Note. A regular expression should not be used if other options are available.

+1
source

3 ? (, )

+1

You can do both in the same regular expression (guessing zip code - numbers).

('1234567').match(/^\d{5,}$/) // ['1234567']
('1234').match(/^\d{5,}$/) // null
('12 34').match(/^\d{5,}$/) //null
('123 4567').match(/^\d{5,}$/) //null

therefore instead of:

if((val.length >= 5) && (*******)){
    //code
}

using:

if(val.match(/^\d{5,}$/)) {
    //code
}
+1
source

Source: https://habr.com/ru/post/1777881/


All Articles