Click to See Complete Forum and Search --> : Form Validation - Please Help!!!!


blessedame
07-16-2003, 07:05 AM
Hi Guys,

I need your help with this one...

This is the scenario,
the serial number entered in this form could be:

12 characters long - but 5th digit must be 4 or 6
and nowhere can the character be anything but 0123456789abcdefghABCDEFGH


15 characters long - first 3 digits must be '151' and the rest anything between 0123456789abcdefghABCDEFGH


This has already been done in the code below.
(was done way before my time so I couldnt even decipher that) :-)


Now we have a new scenario (including what we currently have):

Serial number could be 12 characters long - first digit must be a 6 and 2nd character must be D,E or F.

This new scenario is what I need to include in the code.


This is the script I currently have:

<SCRIPT LANGUAGE="JavaScript">

function validateAll(form) {
serialnumber = trim(form.serialnumber.value) ;

if(serialnumber == ""){
alert("Please enter your serial number");
return false ;
}else if( ! validatefig(serialnumber) ){
return false ;
}else if( ! validatefigNew(serialnumber) ){
return false ;
}
return true ;
}

function trim(strText) {
// this will get rid of leading spaces
while (strText.substring(0,1) == ' ')
strText = strText.substring(1, strText.length);

// this will get rid of trailing spaces
while (strText.substring(strText.length-1,strText.length) == ' ')
strText = strText.substring(0, strText.length-1);

return strText;
}

// validating serial numbers
function validatefig(serialnumber) {
if (isIndexOf(serialnumber, "-+/? !@#$%^&*()_~`:;") ) {
alert("Invalid Serial Number, please try again");
return false;
}
var numeric = "0123456789abcdefghABCDEFGH" ;
for (i=0; i<serialnumber.length; i++) {
if (numeric.indexOf(serialnumber.charAt(i))==-1){
alert("Invalid serial number, please try again");
return false;
}

}
return true;

}

function validatefigNew(serialnumber) {
if (serialnumber.length == 12) {
if (serialnumber.charAt(4) == '4') {
return true;
} else if (serialnumber.charAt(4) == '6') {
return true;
} else {
alert("Invalid Serial Number, please try again");
return false;
}
}else if (serialnumber.length == 15){
if (serialnumber.substr(0,3) == '151'){
return true;
}else {
alert("Invalid Serial Number, please try again.");
return false;
}
}else {
alert("Invalid Serial Number, please try again.");
return false;
}
}
function isIndexOf(field, characters) {
for (var i = 0; i < characters.length; i++) {
if (field.indexOf(characters.charAt(i)) != -1) {
return true;
}
}
return false;
}


I hope this all make sense.

Thanks everyone.

pyro
07-16-2003, 07:34 AM
You are going to want to use regexp:

1:/^[0-9a-g]{4}[46]{1}[0-9a-g]{7}$/i

2:/^151[0-9a-g]{13}$/i

3:/^6[D-F].{10}$/

That will also make your code a lot shorter...

blessedame
07-16-2003, 07:45 AM
:confused:

Pyro, thanks for the response.
I am a total rookie in Javascript, I dont even have a clue what
regexp means.

More help please.

Thanks!

pyro
07-16-2003, 07:50 AM
Sorry, regexp stands for regular expressions ( http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1193136 ).

Here's how to impliment that code:

<script type="text/javascript">
function validate(frm) {
if (!/^[0-9a-g]{4}[46]{1}[0-9a-g]{7}$/i.test(frm.re1.value)) {
alert ("1 is invalid");
return false;
}
else if (!/^151[0-9a-g]{13}$/i.test(frm.re2.value)) {
alert ("2 is invalid");
return false;
}
else if (!/^6[D-F].{10}$/i.test(frm.re3.value)) {
alert ("3 is invalid");
return false;
}
else {
return true;
}
}
</script>


</head>

<body>
<form name="myform" onsubmit="return validate(this)">
1: <input type="text" name="re1">
2: <input type="text" name="re2">
3: <input type="text" name="re3">
<input type="submit" value="Submit">
</form>

blessedame
07-16-2003, 08:30 AM
Thank you.

I tried it just using the if part of the statement and it worked perfectly.

But when I tried the whole thing, it bombed.

The thing is, I only have re1. Just the one textbox to be validated against.

So I tried:

<script type="text/javascript">
function validate(frm) {
if (!/^[0-9a-g]{4}[46]{1}[0-9a-g]{7}$/i.test(frm.re1.value)) {
alert ("1 is invalid");
return false;
}
else if (!/^151[0-9a-g]{13}$/i.test(frm.re1.value)) {
alert ("2 is invalid");
return false;
}
else if (!/^6[D-F].{10}$/i.test(frm.re1.value)) {
alert ("3 is invalid");
return false;
}
else {
return true;
}
}
</script>


</head>

<body>
<form name="myform" onsubmit="return validate(this)">
1: <input type="text" name="re1">
<input type="submit" value="Submit">
</form>

But this didnt work.

blessedame
07-16-2003, 01:21 PM
Guys,

I am really stuck with this one.

Could any one help please.

Thanks!