Current location: Hot Scripts Forums » General Web Coding » JavaScript » Form Validation - Prevent submit if fields are blank


Form Validation - Prevent submit if fields are blank

Reply
  #1 (permalink)  
Old 03-15-05, 07:47 AM
Cepeleon Cepeleon is offline
Newbie Coder
 
Join Date: Jan 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Form Validation - Prevent submit if fields are blank

Hi,

I need to validate a form when the user hits submit.

Basically I want to say that if certain fields on the form have a value of " " then display a message box and prevent the form being submitted to the ASP file ELSE submit the form normally.

Now I came up with something but its not working because when I hit submit with the fields blank its being sent to the process.asp file with not message box appearing.

What is wrong with what I am doing?

In the header I put this,

Code:
function checkIt(f)
{   
if (f.elements['Field1name'].value=="" && f.elements['Field2name'].value=="")   
{      
alert('You have not specified the products you wish to order please go back to step 2');
     return false;   
}   
return true;
}
In the body I put this,

Code:
<form method="post" action="process.asp" name="sForm" onSubmit="return checkIt(this);">
...
Any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-15-05, 04:51 PM
tusamni tusamni is offline
New Member
 
Join Date: Jun 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
I am also looking for a solution to a similar problem...

Consider this a free bump..

-Erik
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-15-05, 05:33 PM
djwayne_2004 djwayne_2004 is offline
Newbie Coder
 
Join Date: Jan 2005
Location: England
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

To fix your problem change your form tag value to:

Code:
<form method="post" action="process.asp" name="sform" onSubmit="return checkForm(this); return false;">
And change your javascript to:

Code:
function checkForm(f)
{
    if (f.elements['FieldName1'].value == "" && f.elements['FieldName2'].value == "")
    {
        alert("You have not specified the products you wish to order please go back to step 2");
        return false;
    }
    else
    {
        f.submit();
        return false;
    }
}
This should now stop the form from being submitted unless both fields have something in them. Hope this helps.

Wayne
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make read-only form fields Ashantai JavaScript 4 02-15-05 05:26 PM
Flexible form validation question epetoke JavaScript 6 09-12-04 05:19 PM
formmail problem gscraper Perl 12 08-27-04 04:06 AM
Form validation - required fields Funky_Monk JavaScript 1 08-16-04 11:26 AM
displaying fields on the same form aliasgar ASP 1 01-22-04 01:50 PM


All times are GMT -5. The time now is 10:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.