Click to See Complete Forum and Search --> : validating a field


TheOnlyCreature
01-14-2003, 11:56 AM
Hi,

I was trying to validate a field for the past two days now, but I can't get it to work. So if anyone here can help me, I'd be really thankfull.

It's about a field containing 4 digits and 2 letters, like this: 1234AB. I can't seem to get it done. The closest I was was a script that checked if the first item was a digit, and then the second, and then the third, end the fourth. After that it checked if the fifth was a letter, and after that the sixth. After more than two screens of 'ifs' it didn't work :(

Thanks in advance

Dan Drillich
01-14-2003, 12:11 PM
You should use a regular expression for it.
Let me try to put it together.

Cheers,
Dan

TheOnlyCreature
01-14-2003, 12:17 PM
That would be nice. I tried some scripts from other pages, but I couldn't get it done. This isn't easy.

Dan Drillich
01-14-2003, 12:21 PM
Try this -



<head>

<SCRIPT LANGUAGE="JavaScript">
var exp = "[0-9][0-9][0-9][0-9][A-Z][A-Z]";
var text = "1234AB";
var result = text.match(exp);

</SCRIPT>


</head>

<body>

<table>

<tr>
<td>
</td>
</tr>

</table>


<SCRIPT LANGUAGE="JavaScript">

document.write(result)

</SCRIPT>


</body>




Cheers,
Dan

TheOnlyCreature
01-14-2003, 12:44 PM
I'm afraid I wasn't clear enough the first time. (Or I'm working too long and can't see the answer anymore)

I have a script (it's a school assignment) wich needs your name, adress and town. Those fields may NOT be empty. I had that part programmed easily. Next up is your postcode (zip in dutch). It should NOT allow anything that is NOT a dutch zip. Dutch zips have the following format: 9999XX , where 9 is any number and X is any letter.

If the script is hard to read, it's because some parts are in dutch, but i'll try and translate most of it.

<html>

<head>
<title> Jeffrey Hendriks CSV1B </title>
</head>

<body>
<FORM NAME="Invoer">
<INPUT TYPE="text" NAME="naam"> Enter name.<br>
<INPUT TYPE="text" NAME="adres"> Enter adress<br>
<INPUT TYPE="text" NAME="woonplaats"> Voer uw woonplaats in.<br>
<INPUT TYPE="text" NAME="postcode"> Enter zip.<br>
<INPUT TYPE="text" NAME="studierichting"> Enter study you are following.<br>
<br>
<INPUT TYPE="submit" VALUE="Bewerk gegevens" NAME="startCheck" onClick="checkNAW()">
<INPUT TYPE="reset" VALUE="Leeg maken" NAME="maakLeeg">
</FORM>
</body>

<script language="JavaScript">
<!--

function checkNAW()
{
if (Invoer.naam.value == "")
{
alert("Please enter a name.");
Invoer.naam.focus();
return (false);
} else {
if (Invoer.adres.value == "")
{
alert("Please enter an adress.");
Invoer.adres.focus();
return (false);
} else {
if (Invoer.woonplaats.value == "")
{
alert("Please enter a town.");
Invoer.woonplaats.focus();
return (false);
} else {
return (true);
}
}
}
}

//end hiding -->

</script>

</html>


I hope this makes it more clear. I could really use some help with this. Thanks in advance

swon
01-14-2003, 01:19 PM
I think that does the trick:

<html>

<head>
<title> Jeffrey Hendriks CSV1B </title>
</head>

<body>
<FORM NAME="Invoer" onSubmit="return checkNAW()">
<INPUT TYPE="text" NAME="naam"> Enter name.<br>
<INPUT TYPE="text" NAME="adres"> Enter adress<br>
<INPUT TYPE="text" NAME="woonplaats"> Voer uw woonplaats in.<br>
<INPUT TYPE="text" NAME="postcode"> Enter zip.<br>
<INPUT TYPE="text" NAME="studierichting"> Enter study you are following.<br>
<br>
<INPUT TYPE="submit" VALUE="Bewerk gegevens" NAME="startCheck">
<INPUT TYPE="reset" VALUE="Leeg maken" NAME="maakLeeg">
</FORM>
</body>

<script language="JavaScript">
function checkNAW()
{

zipcode = Invoer.postcode.value;
result = zipcode.match(/[0-9][0-9][0-9][0-9][A-Z||a-z][A-Z||a-z]/);

if (Invoer.naam.value == "")
{
alert("Please enter a name.");
Invoer.naam.focus();
return false;
}

if (Invoer.adres.value == "")
{
alert("Please enter an adress.");
Invoer.adres.focus();
return false;
}

if (Invoer.woonplaats.value == "")
{
alert("Please enter a town.");
Invoer.woonplaats.focus();
return false;
}

if(result == null)
{
alert("Please enter a correct zipcode.");
Invoer.postcode.focus();
return false;
}
if (Invoer.studierichting.value == "")
{
alert("Please enter a your S....");
Invoer.studierichting.focus();
return false;
}
else {
return true;
}


}
</script>

TheOnlyCreature
01-14-2003, 01:28 PM
Thank you SO much! That was relatively easy too! I am not worthy!

* falls down on knees and bows head to ground *

Tricky123
02-13-2003, 09:43 AM
Can I make a suggestion or alert you to a problem with using name.value = "" ??

Try entering a space bar or tab key into your input field and see if the form submits. It will, even though there is in reality no data!

Is there a solution to this?