...

View Full Version : Client side validation with javascript



siege
03-04-2005, 03:12 PM
Hi all, I am new here and new in JavaScript as well. Well, I need to do client side validation using JavaScript to check fields:

1)The userID of a user. Lets say the userID is S1234567D, I must make sure the first letter is either a "s" or a "S" and the middle characters are all digits only and the last character is a letter from A-Z

2)I also need to check postal code of the user to make sure its all digits, no other character is allowed.

3)Contact no. The first digit must be either 6 , 8 or 9 and followed by 7 digits.

I have done some coding on those requirements but after running the codes, they dont work. As in, when i view it in browser the window alerts that I coded did not pop up when they are supposed to. I posted most of the script because I cant find the problem even after hours of solving. Guys please help if you can. Thank you.



//nricFirst variable to store the first character of the nric
//nricCentre variable to store the middle characters/digits of the nric
//nricLast to store the last character of the nric
//nricNo is the string of characters that are allowed for the middle string (digits)

nric=txtNric.value;
var nricFirst, nricCentre, nricLast, nricNo;

nricFirst = nric.charAt(0);
nricFirst.ToUpper();
nricCentre = nric.substr(1,7);
nricNo = new String("0123456789");


if(nric == "") {
window.alert("Please enter a valid NRIC");
}
if(nricFirst != "S"){
window.alert("Please enter NRIC starting with S");
}

//intIndex to store the loop variable
//strCurrentChar to store the variable(digit) to check with the valid string
//of digits nricNo

var intIndex;
var strCurrentChar;



if (nricCentre.length != 7){

for (intIndex=0; intIndex<=nricCentre.length;intIndex++){
strCurrentChar = nricCentre.charAt(intIndex);
if (nricNo.indexOf(strCurrentChar)==-1){
window.alert("NRIC numbers incorrect");
}
}
}

nricLast = nricNo.charAt(8);

var nricAlphabets, intChar;
nricAlphabets = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

for(intChar=0;intChar<=nricAlphabets.length;intChar++) {
if(nricLast == nricAlphabets(intChar)){
return true;
//else {
//window.altert("Last letter not valid");
//}
}
}


//intIndex to store array index
//intMaxIndex to store max index of the postal code
//strCurrentChar to store the variable(digit) to check with the valid string
//postalNostr to store string of valid digits

postal=txtPostal.value;
var intIndex;
var intMaxIndex;
var strCurrentChar;
var postalNostr;

postalNostr = new String("0123456789");

if(postal.length != 6 ) {
window.alert("Please enter 6 digits postal code");
else {
intMaxIndex = postal.length - 1;
for(intIndex=0;intIndex<=intMaxIndex;intIndex++){
strCurrentChar = postal.charAt(intIndex);
if(postalNostr.indexOf(strCurrentChar)==-1){
window.alert("Please enter a valid 6 digit postal code")

}
}
}
}



//intIndex to store array index
//intMaxIndex to store max index of the postal code
//strCurrentChar to store the variable(digit) to check with the valid string
//phoneNostr to store string of valid digits

phone=txtPhone.value;
var intIndex;
var intMaxIndex;
var strCurrentChar;
var phoneNostr;

phoneNostr = new String("0123456789");

if(phone.length != 8 ) {
window.alert("Please enter 8 digit phone no.")

else {

intMaxIndex = phone.length - 1;
for(intIndex=0;intIndex<=intMaxIndex;intIndex++){
strCurrentChar = phone.charAt(intIndex);
if(phoneNostr.indexOf(strCurrentChar)==-1){
window.alert("Please enter a valid 6 digit postal code");
}
}
}
}


//intFirst to store first character of phone no.
var intFirst

intFirst = phone.charAt(0);

if(intFirst != 8 || intFirst !=9 || intFirst !=6) {
window.alert("Please enter a phone no. starting from either 6 8 or 9")
}

Philip M
03-04-2005, 06:43 PM
Regular expressions are the best way to go about this.

if (/(^s)(\d+)([a-z]$)/gi.test(userID.value)) { ///userID to be letter s or S followed by at least one digit followed by one single letter ignoring case
***entry is valid - proceed
}

Note: you can replace \d+ by say \d{2,4} to match at least 2 and not more than 4 digits (or whatever) if you wish.

if (/(^\d+$/.test(postcode.value)) { /// match 1 or more digits and nothing but
***entry is valid - proceed
}

Note: use \d{5} in place of \d+ to test for 5 digits only (or whatever)


if (/^(6|7|8)(\d{7}$/.test(contactnumber.value)) { /// 6,7 or 8 followed by 7 digits and nothing else
***entry is valid - proceed
}


Naturally you must add the code to follow if the expression is true (presumably proceed) or false (presumably try again).

.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum