By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 1,151 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

Validating Forms Input

P: n/a
Hi all,

I am looking for a little help validating a forms input. On the form are key
items which I do not wish to be zero length.
I have wrote the following function:

<script language=javascript>
function ValidateLength(oid, min, max)
{
var txt = document.getElementById(oid);
var length = txt.value.length;
if(length == 0)
{
alert("You must enter your reference, Company Name and Contact Name");
}
}
</script>

However their are still a few issues I would like to work out.

I all it with my forms sumbit event with the following:

<input type="submit" name="Submit" onclick="ValidateLength('txtRef',0,100)"
value="Submit Details">

However that will only check the length of the item txtRef, how can I check
more textbox values?
Also how can I stop the form posting if the alert is raised?

Thanks for any help you can provide
Mick
Jan 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Something like this will help
<form id="myform" onsubmit="validate()">
If your "validate" function returns false,form wont be submitted.

Jan 6 '06 #2

P: n/a
sunilp wrote on 06 jan 2006 in comp.lang.javascript:
Something like this will help
<form id="myform" onsubmit="validate()">
If your "validate" function returns false,form wont be submitted.


No, only in:

<form id="myform" onsubmit="return validate()">
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 6 '06 #3

P: n/a
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.109.133.242:
sunilp wrote on 06 jan 2006 in comp.lang.javascript:
Something like this will help
<form id="myform" onsubmit="validate()">
If your "validate" function returns false,form wont be submitted.


No, only in:

<form id="myform" onsubmit="return validate()">


exactly. as long as your onsubmit has 'return' before your function,
then you can prevent the form from being submitted if you return false.

ie: in your validate() function:

if(length == 0)
{
alert("You must enter your reference, Company Name and Contact
Name");
return false;
}
else {
return true;
}

will submit your form if there are no 'errors', but stop submission if
there are.
Jan 6 '06 #4

P: n/a
Good Man wrote on 06 jan 2006 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> wrote in
news:Xn********************@194.109.133.242:
sunilp wrote on 06 jan 2006 in comp.lang.javascript:
Something like this will help
<form id="myform" onsubmit="validate()">
If your "validate" function returns false,form wont be submitted.
No, only in:

<form id="myform" onsubmit="return validate()">


exactly. as long as your onsubmit has 'return' before your function,
then you can prevent the form from being submitted if you return false.

ie: in your validate() function:

if(length == 0)
{
alert("You must enter your reference, Company Name and Contact
Name");
return false;
}
else {
return true;
}


Where I would call an else after a return superfluous
if(theLength == 0){
alert(".... mistake!");
return false;
}
return true;

will submit your form if there are no 'errors', but stop submission if
there are.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.