Forum Moderators: open

Message Too Old, No Replies

Java Required Fields in Form

"Please fill in required field" popup shows, but form still submits

         

bquick

11:12 pm on May 13, 2009 (gmt 0)

10+ Year Member



Hello,

I have a little annoying problem. I want one field in my form to be required. The java script shows "Please Enter a Zip Code" if this field is blank, but it still takes you to the results. Is there a way to make it cancel the "submit" if the required field is not filled out?

Script I'm using:

<head>
<script type='text/javascript'>
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}
</script>
</head>

<form name="search_form" method="get" action="results_list.php" onSubmit="return validateform()" onclick="notEmpty(document.getElementById('req1'), 'Please Enter a Zip Code')"value='Check Field'>

Zip code: <input type="Text" value="" name="zip" size="12" maxlength="7" id='req1'>

<input class="submit" type="Submit" value="Go">
</form>

[edited by: bquick at 11:30 pm (utc) on May 13, 2009]

LifeinAsia

11:19 pm on May 13, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Welcome to Webmaster World!

I'd incorporate the notEmpty function into the validateform function.

And please don't include personal URLs when posting.

bquick

11:37 pm on May 13, 2009 (gmt 0)

10+ Year Member



Do you have an example of what that is supposed to look like and where it's supposed to go?

Because I thought this was my not empty function

<script type='text/javascript'>
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}

</script>

LifeinAsia

12:01 am on May 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That is your not empty function. But you're calling it directly with an onclick instead of through onSubmit. You could try onclick="return notEmpty(document.getElementById('req1'), 'Please Enter a Zip Code')"

rocknbil

2:47 pm on May 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard bquick!

First, you don't put "onClick" into a form. If you use onClick, it should be an element that you click: anchor, button . . . .

Second, understand the effect of return false in the context of returning false from a function. When you return false from a function, it tells the browser to not allow the element to perform it's natural action.

<a href="#" onClick="return false">I won't navigate anywhere if Javascript is enabled</a>
<form action="" onSubmit="return false">
I won't sumit if Javascript is enabled
</form>

So understanding this concept, you return false from the function. Pass the form object to validateform() and allow Javascript to manage the submit if the fields don't pass validation. There's no reason to return true from this scenario.

By allowing JS to manage the submit, if JS is disabled the form will still work.

Last, you should always try to remove inline JS and CSS to the external style sheets, making for cleaner code and easier maintenance.

Working example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1998/REC-html40-19980424/loose.dtd">
<!-- doctype all on one line -->
<html lang="en">
<head>
<script type="text/javascript">
window.onload=function() {
if (document.getElementById) {
document.getElementById('search_form').onsubmit=function() { return validateform(this); };
}
};
function validateform (form) {
var requireds = Array('req1','req2');
var eng = Array ('postal code','city');
var msg = '';
for (i=0;i<requireds.length;i++) {
if (document.getElementById(requireds[i]).value=='') {
msg += 'Please enter your ' + eng[i] + '.\n';
}
}
if (msg != '') { alert(msg); }
else { form.submit(); }
return false;
}
</script>
</head>
<body>
<form name="search_form" id="search_form" method="get" action="results_list.php">
<p>
<label for="req2">City:</label>
<input type="Text" value="" name="city" id="req2" size="12" maxlength="25">
</p>
<p>
<label for="req1">Zip code:</label>
<input type="Text" value="" name="zip" id="req1" size="12" maxlength="7">
</p>
<p><input class="submit" type="Submit" value="Go"></p>
</form>
</body>
</html>

bquick

5:46 pm on May 14, 2009 (gmt 0)

10+ Year Member



Thank you! I appreciate your help. The reason it wasn't working was because I was calling to a .js file and trying to add coding directly into the HTML. I was trying to use the same .js file for all forms and when I wanted only one form to require zip code information I was trying to do a work around. Once I added your script into the original .js it worked. I supposed I'll just have to have two .js files. Thank you again!