Reputation: 5
I'm a beginner writing JQuery to add to a customization form website. Most of the options are drag and drop, but I have to write custom jquery in some cases.
For this, I've been able to figure out to validate a nine-character string so that an error message is presented if the string is NOT 9 characters long, and if it starts with anything other than "B", "E", or "N."
However, it also needs to check and make sure that all other characters after the first is a digit. For instance, an acceptable user input would be e00012345.
What is the simplest way to do this?
// this validation will check to make sure that an accepted value is entered into a field.
// currently, the validation is not perfect. Ideally, the value would start with a specific character (n, b or e) and 8 digits. Right now it just must start with n, b or e and be 9 characters long.
$(function() {
// for the Missouri Business Number -- on blur, if the value is 9 characters long and starts with b, e, or n (uppoer or lower case), then the input is valid. Otherwise, error messages appear.
$("input#id_wrT4duNEOW").blur(function() {
if (
(($("#id_wrT4duNEOW").val().startsWith("b")) &&
($("#id_wrT4duNEOW").val().length == 9)) ||
(($("#id_wrT4duNEOW").val().startsWith("e")) &&
($("#id_wrT4duNEOW").val().length == 9)) ||
(($("#id_wrT4duNEOW").val().startsWith("n")) &&
($("#id_wrT4duNEOW").val().length == 9)) ||
(($("#id_wrT4duNEOW").val().startsWith("B")) &&
($("#id_wrT4duNEOW").val().length == 9)) ||
(($("#id_wrT4duNEOW").val().startsWith("E")) &&
($("#id_wrT4duNEOW").val().length == 9)) ||
(($("#id_wrT4duNEOW").val().startsWith("N")) &&
($("#id_wrT4duNEOW").val().length == 9))
)
{
// good things happen
}
else {
// error message
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
EDIT
Okay, I tried adding in the regex line, but I'm not getting the result. What am I missing?
$(function() {
$("input#id_wrT4duNEOW").blur(function() {
const regex = /^[bBeEnN]{1}[0-9]{8}$/
var mobiz = $("#id_wrT4duNEOW").val();
if (console.log(regex.test(mobiz)))
{
// good things happen
}
else {
// error message
}
});
});
Upvotes: 0
Views: 399
Reputation: 32176
Regex to the rescue. It's pretty straightforward to do using a regex and its associated .test
method. The following regex ensures the string starts with one of the characters b
, e
, or n
(not case sensitive), followed by exactly 8 digits:
test1 = "B12345678";
test2 = "N123456789";
test3 = "x12345678";
const regex = /^[bBeEnN]{1}[0-9]{8}$/
console.log(regex.test(test1))
console.log(regex.test(test2))
console.log(regex.test(test3))
So, for your snippet, you could adapt it like this:
$(function() {
$("input#id_wrT4duNEOW").blur(function() {
var val = $("#id_wrT4duNEOW").val();
if (/^[ben]{1}\d{8}$/i.test(val)) {
// good things happen
} else {
// error message
}
});
});
Upvotes: 1
Reputation: 24965
//1) must be 9 characters
//2) first character must be B, E, or N
//3) 8 characters past the first must be digits
var pattern = /^(B|E|N)\d{8}$/
console.log(pattern.test("weee"));
console.log(pattern.test("B12345678"));
console.log(pattern.test("A12345678"));
console.log(pattern.test("B123456789"));
Upvotes: 0