469,642 Members | 1,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

check form data before sending to a server

Hi, all!
I have a problem, how can I check a form data before sending it to
server. I have the example below, but first data is sent to server, but
after JavaScript checks the data :(

I need that JavaSctipt check data and after send it to the server, how
can do that?

Example:
....
<script language="javascript">
function check()
{
if(document.forms[0].name1.value == "")
{
alert("enter name");
return false;
}
else return true;
}
</script>
....
<form action="someapp.php" onSubmit="return check()">
<input type="text" name="name1">
<input type="submit">
</form>
....

Thanks in advance!

Nov 10 '05 #1
2 5078
"Dknight" <dm***********@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi, all!
I have a problem, how can I check a form data before sending it to
server. I have the example below, but first data is sent to server, but
after JavaScript checks the data :(

I need that JavaSctipt check data and after send it to the server, how
can do that?


[snip]

What makes you think that the data is sent to the server first?

"onSubmit()" is invoked when you hit the "submit" button;
only if "check()" returns "true" is "action=" performed.

Your code could be simplified somewhat:

<script type="text/javascript">
function check(form) {
if (form.name1.value == "") {
alert("enter name");
return false;
}
return true;
}
</script>
....
<form action="someapp.php" onSubmit="return check(this)">
<input type="text" name="name1">
<input type="submit">
</form>
....
Nov 10 '05 #2
McKirahan wrote:
"Dknight" <dm***********@hotmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Hi, all!
I have a problem, how can I check a form data before sending it to
server. I have the example below, but first data is sent to server, but
after JavaScript checks the data :(

I need that JavaSctipt check data and after send it to the server, how
can do that?

[snip]

What makes you think that the data is sent to the server first?

"onSubmit()" is invoked when you hit the "submit" button;
only if "check()" returns "true" is "action=" performed.

Your code could be simplified somewhat:

<script type="text/javascript">
function check(form) {
if (form.name1.value == "") {


Or:

if ( !form.name1.value ) {

alert("enter name");
return false;
}
return true;


There is no need to return true, just don't return false.

[...]
--
Rob
Nov 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Gregory A Greenman | last post: by
3 posts views Thread by Kathy | last post: by
7 posts views Thread by Mark Waser | last post: by
2 posts views Thread by Nuno Magalhaes | last post: by
7 posts views Thread by GeorgeAtkins | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.