...

View Full Version : Javascript Numerical Field Validation



Gyto
01-30-2007, 01:07 PM
Hi there,

I'm trying to validate a form field to only allow numerical values when the form is submitted. It appears to work ok but after it has detected a blank or invalid entry in the field and displayed the corresponding error message, it is still submitting the form. The 'return false;' statements do not appear to be working.....any ideas?

Function script:


<script language="javascript">
<!--
var error = "";
var submitted = 0;

function Validator(form1)
{
var digits = "0123456789";

if (submitted)
{
alert("Form already submitted, please wait");
return false;
}

if (form1.jobref.value == "")
{
error += "Please enter a Job Reference number.\n";
}
for (var i = 0; i < form1.jobref.value.length; i++)
{
temp = form1.jobref.value.substring(i, i+1)

if (digits.indexOf(temp) == -1 && form1.jobref.value != "")
{
error += "The Job Reference number you entered is invalid.\n";
break;
}
}

if (error != "")
{
alert(error);
return false;
}
else if (!submitted)
{
submitted = 1;
return true;
}
}

-->
</script>

Form Code:


<form id="form1" name="form1" method="post" action="ITJobReport.asp">

<input name="jobref" type="text" id="jobref" />

<input type="submit" name="Submit" value="Submit" style="width:130px" onclick="Validator(form1)"/>



I have also tried using the following, but it stops the form being submitted at all and just displays the "The Job Reference number you entered is invalid" message:

onclick="return Validator(form1)"

Thanks

vwphillips
01-30-2007, 01:31 PM
<form id="form1" name="form1" onsubmit="return Validator(this);" method="post" action="ITJobReport.asp">

<input name="jobref" type="text" id="jobref" />

<input type="submit" name="Submit" value="Submit" style="width:130px" />

Gyto
01-30-2007, 01:38 PM
<form id="form1" name="form1" onsubmit="return Validator(this);" method="post" action="ITJobReport.asp">

<input name="jobref" type="text" id="jobref" />

<input type="submit" name="Submit" value="Submit" style="width:130px" />

Ah I see what I did....d'oh.....thanks very much, worked perfectly :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum