Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2010, 12:19 AM   PM User | #1
Iceman Luch
New to the CF scene

 
Join Date: Dec 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Iceman Luch is an unknown quantity at this point
Stopping submit if form invalid

I wrote a form and a JavaScript to valid the form. I cannot figure out however how to stop the form from submitting if the form is invalid.

[CODE]
function validateForm()
{

if(""==document.test.custName.value)
{
alert("Please enter your name.");
return false;
}

if(""==document.test.email.value)
{
alert("Please enter your email address.");
return false;
}

if(""==document.test.custComment.value)
{
alert("Please enter your comment.");
return false;
}

return true;
}
[CODE]
Iceman Luch is offline   Reply With Quote
Old 12-24-2010, 01:10 AM   PM User | #2
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Try putting the if's the other way around.

Code:
if(document.test.custComment.value === "")
EXAMPLE:

Code:
function validateForm() {
  var e=false,w=[];
  if(document.test.custName.value === '') {
    w[0] = "Please enter your name.";
    e = true;
  }
  if(document.test.email.value === '') {
    w[1] = "Please enter your email address.";
    e = true;
  }
  if(document.test.custComment.value === '') {
    w[2] = "Please enter your comment.";
    e = true;
  }
  if(e) {
    alert(w.join("\n"));
    return false;
  }
}
EDIT:
Are you using the function in a button onclick action or the form onsubmit action?
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 12-24-2010 at 01:19 AM..
DJCMBear is offline   Reply With Quote
Old 12-24-2010, 03:00 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Arrow

Quote:
Originally Posted by Iceman Luch View Post
I wrote a form and a JavaScript to valid the form. I cannot figure out however how to stop the form from submitting if the form is invalid.

Code:
function validateForm() {
  if(""==document.test.custName.value) {
    alert("Please enter your name.");
    return false;
  }
  if(""==document.test.email.value) {
    alert("Please enter your email address.");
    return false;
  }
  if(""==document.test.custComment.value) {
    alert("Please enter your comment.");
    return false;
  }
  return true;
}
If using <form> tag:
Code:
<form name="my_form" action='' onsubmit="return validateForm">
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 07:26 AM.


Advertisement
Log in to turn off these ads.