I have a problem creating a regular expression:
I have to validate that a string does not accept two or more blanks.
Example: ABCabc aasbdfdf% $ && $$ / $ /
If the user types a string as the above example shows a modal " Not a valid name
":
This is my code:
var newexprg = /^(?!\s*$)[-a-zA-Z0-9_:,.' ']{1,100}$/;
var obj= {
descripcion: des,<--- 'ABCabc $%/##%%aass'
nombre: $("#txtNombre").val(), <--- 'ABCabc $%/##%%aass'
}
if (newexprg.test(obj.nombre)) {
if (newexprg.test(obj.descripcion)) {
$('#modal-Caracteres').modal("show");
} else {
$.ajax({
type: "POST",
url: 'MyMethodSave',
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{ParameterName:" + JSON.stringify(obj) + "}",
async: false,
success: function (response) {
},
});
}
} else {
$('#modal-Caracteres').modal("show");
}
But every time I enter my chain such method saves the object.
A RegEx to match with a negative condition, such as " it does not contain 2 whitespaces " is possible, but there's a much easier approach to this:
Match a string with with 2 whitespaces:
/\s\S*\s/
\\s
followed by any number of characters that are not whitespace \\S*
and followed by a second whitespace \\s
.And then negate the value returned by the match:
var newexprg = /\s\S*\s/;
if (! /\s\S*\s/.test(yourString)) {
// Valid
} else {
// Invalid
}
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.