Reputation: 5856
i have the following validation on a form field:
$(".controlscard input").blur(function() {
var re = /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i;
if(re.test(document.getElementById("register_coachcard_no").value))
{
$(this).css('border-color','green');
$(this).siblings('.info').css('display','none');
$(this).siblings('.error').css('display','none');
$(this).siblings('.valid').css('display','inline-block');
$("#email_error40111").hide();
}
else
{
$(this).css('border-color','red');
$(this).siblings('.info').css('display','none');
$(this).siblings('.valid').css('display','none');
$(this).siblings('.error').css('display','inline-block');
$('#registerErrors').show();
$('#email_error40111').remove();
$('#registerErrors').append('<p id="email_error40111">Coachcard must contain at least 1 letter and 1 number</p>');
}});
The above basically checks if the entered content contains 1 letter and 1 number.
I need to put something in place where, if the value length is 8 characters, it will need to check for upto 2 letters, and then 6 numbers.
If the value length is 9 characters, then only numbers can be entered.
Any suggestions on what i can do?
Cheers, Dan
Upvotes: 1
Views: 865
Reputation: 20225
I think you want a regex like this:
/^[a-zA-Z]\d|[a-zA-Z]{2}\d{6}|\d{9}$/i
The |
indicates a logical OR, so if one of these holds, it will match:
You mentioned that you wanted to match 1 or 2 letters in the second case, but I wasn't sure how this fit with your requirement of 8 characters. You could modify it to accept 1 or 2 letters for 7 or 8 total characters like so:
/^[a-zA-Z]\d|[a-zA-Z]{1,2}\d{6}|\d{9}$/i
Edit:
From comments, adjusted regex:
/^[a-zA-Z]\d|[a-zA-Z]{1}\d{7}|[a-zA-Z]{2}\d{6}|\d{9}$/i
Upvotes: 1