0

I am writing a javascript code usign regex such that it validate license plate. I want a pater for my license plate is 6-8 digit that is not more than 8 and not less than 6 it would be 6 or 7 or 8. But digit should be alphanumeric so I tried function as.

<!----------LICENSE PLATE---------->
var strFilter = /^[A-Za-z0-9]$/;
var obj = document.getElementById("licenseplate");

if ((!strFilter.test(obj)) || (obj.length < 6) || (obj.length > 8)){
    alert("Please enter valid 6-8 digit license plate.");
    obj.focus();
    obj.style.background = "#DFE32D";
    obj.value = "";
    return false;
}

It gives me error on all condition that if all are numeric or all are alphabetic or are combination of both.

2
  • What on earth is an HTML comment doing in JavaScript code?! Commented Dec 12, 2011 at 4:42
  • As I was following patter on my pages so I used it... Well Its inpage validation that may be second reason for using HTML comment. Commented Dec 12, 2011 at 4:44

2 Answers 2

3

Something like:

/^[0-9A-Za-z]{6,8}$/
Sign up to request clarification or add additional context in comments.

2 Comments

12222222, 122AAA22,5566AVA or anything else Its giving me error.
try document.getElementById("licenseplate").value
1

obj.length is not right... obj, as returned by document.getElementById() is a DOM node, not a String. Assuming licenseplate is a form field:

var strFilter = /^[0-9A-Za-z]{6,8}$/;
var obj = document.getElementById("licenseplate");
if (!strFilter.test(obj.value)) {
    alert("Please enter valid 6-8 digit license plate.");
    obj.focus();
    obj.style.background = "#DFE32D";
    obj.value = "";
    return false;
}

This works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.