Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > check form data before sending to a server

Reply
Thread Tools

check form data before sending to a server

 
 
Dknight
Guest
Posts: n/a
 
      11-10-2005
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!

 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      11-10-2005
"Dknight" <> wrote in message
news: 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>
....


 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      11-10-2005
McKirahan wrote:
> "Dknight" <> wrote in message
> news: 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie question - Can a kludgy page be modified before loading and sending back to server ? Attila.Iskander HTML 14 09-22-2011 09:58 PM
Pop-up form sending data to the calling form? Bjorn Sagbakken ASP .Net 4 06-30-2007 03:47 PM
email form, force user to check box before sending lrbtav@gmail.com HTML 1 10-04-2006 06:47 PM
Check form before unload for changes Assimalyst ASP .Net 2 08-11-2005 09:48 AM
How do I change data retrieved from db before sending it out to client Raakeli ASP .Net Web Services 1 03-21-2005 06:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57