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
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?
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
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
Reply With Quote
  #3 (permalink)  
Old
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
Reply With Quote
  #4 (permalink)  
Old
New Member
 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Hope this Help

Try this


Code:
<html>
	<head>
		<script type="text/javascript">
			function formValidation(){
				var selectForm = document.myForm; // Selecting Form
				var formLength = selectForm.elements.length; // getting total no. of elements in it
				for(var x=0; x<=formLength; x++){
					var usingLoop = selectForm.elements[x]; // 
					if(usingLoop.value == 0){ // Checking throug Loop
						return false;
					}
				}
			}
		</script>
	</head>
	<body>
		<form action="<?php $_SERVER['PHP_SELF'];?>" name='myForm' method='POST'>
			<input type="text" name='t1'/>
			<input type="text" name='t2'/>
			<input type="text" name='t3'/>
			<input type="submit" onclick='formValidation();'/>
		</form>
	</body>
</html>
Reply With Quote
  #5 (permalink)  
Old
New Member
 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Use "OR" Operator rather than "AND"

You should use "or" operator (||) at the place of "and" operator(&&). When you are using "and" operator then that means alert will be shown only if both the condition matches( if both text fields will be left empty) otherwise it will return true and asp page will be displayed. On the other hand if you are using "or" operator alert will be shown even if one of the field will be left empty(and also when both of the field will be left empty).
Your Expression will go like:
if (f.elements['Field1name'].value=="" || f.elements['Field2name'].value=="")
in place of:
if (f.elements['Field1name'].value=="" && f.elements['Field2name'].value=="")
Reply With Quote
  #6 (permalink)  
Old
New Member
 
Join Date: Feb 2013
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Use "OR" Operator(||) at the pace of "AND"(&&)
Use expression:
if (f.elements['Field1name'].value=="" || f.elements['Field2name'].value=="")
in place of:
if (f.elements['Field1name'].value=="" && f.elements['Field2name'].value=="")
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 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 04:26 PM
Flexible form validation question epetoke JavaScript 6 09-12-04 04:19 PM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Form validation - required fields Funky_Monk JavaScript 1 08-16-04 10:26 AM
displaying fields on the same form aliasgar ASP 1 01-22-04 12:50 PM


All times are GMT -5. The time now is 03:00 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.