468,509 Members | 1,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,509 developers. It's quick & easy.

Validating Forms Input

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
4 1096
Something like this will help
<form id="myform" onsubmit="validate()">
If your "validate" function returns false,form wont be submitted.

Jan 6 '06 #2
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
"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
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.

Similar topics

5 posts views Thread by The Plankmeister | last post: by
10 posts views Thread by Samir | last post: by
6 posts views Thread by sakms | last post: by
reply views Thread by Gary Shell | last post: by
6 posts views Thread by Advo | last post: by
4 posts views Thread by easoftware | last post: by
1 post views Thread by =?Utf-8?B?bGpsZXZlbmQy?= | last post: by
3 posts views Thread by William Gill | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.