I have this expression:
$(document).ready(function(){
$.validator.addMethod(
"regex",
function(value, element) {
return this.optional(element) || /^(?!.*www)(?!.*http)(?!.*@)(?!.*\.com)(?!.*\.pt)(?!.*co\.uk).+$/i.test(value);
},
"Description field can not include e-mail and/or urls."
);
$("#regForm").validate();
});
If I have a text like this, I get an error, because of the line break
A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e com preços muito acessíveis(50% de desconto no atacado).
Ha mais de 10 anos no mercado de folheados.
Produtos de excelente qualidade.
If I have this text, without line breaks, it works fine:
A Loja virtual possui uma vasta linha de bijouterias folheadas a ouro e prata. São produtos bastante procurados e com preços muito acessíveis(50% de desconto no atacado).Ha mais de 10 anos no mercado de folheados.Produtos de excelente qualidade.
How can I fix this?
.
does not match linebreaks by default. Usually, the s
(called singleline
or dotall
) modifier changes that. Unfortunately, it is not supported by JavaScript.
There is (a slightly verbose) trick to get around that. The character class [\s\S]
matches any space and any non-space character. I.e. any character. So you would need to go with this:
/^(?![\s\S]*www)(?![\s\S]*http)(?![\s\S]*@)(?![\s\S]*\.com)(?![\s\S]*\.pt)(?![\s\S]*co\.uk)[\s\S]+$/i
Alternatively, (only in JavaScript) you can use the "candle operator" [^]
which also matches any single character (since it's a negation of the empty character class, i.e. it matches any character not in the empty set). Whether you find that more or less readable is a matter of taste I guess:
/^(?![^]*www)(?![^]*http)(?![^]*@)(?![^]*\.com)(?![^]*\.pt)(?![^]*co\.uk)[^]+$/i
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With