Hi all,
I am attempting to do some validation on a field on a form for the web, that has to be input a certain way on it. The format is XX-XXXXXXX. I validated the position, length, and if the dash (“-”) does not exist in the data. The followwing code is for those:
var form = document.forms[0];
//********************************
// check the invalid ID entered
//********************************
var chkFormat=form.ID.value;
var is_dash_ok=chkFormat.indexOf(‘-’);
var where_is_dash=chkFormat.indexOf(‘-’);
var length_of_data=chkFormat.length;
//check to see if there is a dash at all in the data
if (is_dash_ok==-1) {
alert("There is NO dash in the entered data. You must enter an ID in this format: XX-XXXXXXX");
form.ID.focus();
return false;
}
//check to see if the dash is in the correct place in the data
if (where_is_dash!=2) {
alert("The dash is in the WRONG position. You must enter an ID in this format: XX-XXXXXXX");
form.ID.focus();
return false;
}
//check to see if the dash is in the correct place in the data
if (length_of_data!=10) {
alert("The length of the data is wrong . You must enter an ID in this format: XX-XXXXXXX");
form.ID.focus();
return false;
}
All of the above code works fine. Now, after this I am trying to verify its mask/pattern of the field. To find out the mask I am trying something like this (the id’s have to be numeric):
// Checks for the following valid ID format invalid entries:
// XX-XXXXXXX
var IDStr = form.ID.value;
var IDPat = /^\1(\d{2}|(-)|\d{7})$/;
var matchArray = IDStr.match(IDPat); // is the format ok?
if(matchArray == null){
alert("ID must be in the format XX-XXXXXXX")
form.ID.focus();
return false;
}
// Build array for invalid dummy ID values
var x;
var myid = new Array();
myid[0] = "12-3456789";
myid[1] = "98-7654321";
myid[2] = "00-0000000";
myid[3] = "11-1111111";
myid[4] = "22-2222222";
myid[5] = "33-3333333";
myid[6] = "44-4444444";
myid[7] = "55-5555555";
myid[8] = "66-6666666";
myid[9] = "77-7777777";
myid[10] = "88-8888888";
myid[11] = "99-9999999";
for (i = 0; i <= 11; i++) {
alert("i value: "+i);
for (x in myid) {
alert("x value: "+x);
{
if (form.ID.value== x ) {
alert("You must enter a VALID ID.");
form.ID.focus();
return false;
}
}
}
}
But the pattern is not coming out correctly. I have not written very much javascript, so any thoughts or help is appreciated.
Thanks,
Mick