Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stop a form being submitted 3

Status
Not open for further replies.

xperience3

IS-IT--Management
Aug 7, 2007
7
GB
Hi,

I'm using this code:

function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return false;
}
return true;
}

and:

<input type="submit" name="submit" value="Continue to select you flight package" onclick="isEmpty(document.getElementById('customersname'), 'Please Fill in the form correctly')", onmouseover="return validate(terms)"/>


to check if a field on a web page has been filled in before submitting.

Only problem is, at the moment it gives the error but still submits the form.

How can I make it NOT submit the form if the error is shown?

Cheers

Simon
 
A return is missing, I guess

Code:
<input type="submit" name="submit" value="Continue to select you flight package" onclick="[COLOR=#ff0000]return[/color]
 isEmpty(document.getElementById('customersname'), 'Please Fill in the form correctly')", onmouseover="return validate(terms)"/>
 
Sorry - another question, I also need to check some other fields arent' empty too.

I guess I need to amend this line:

<input type="submit" name="submit" value="Continue to select you flight package" onclick="return isEmpty(document.getElementById('customersname'), 'Please Fill in the form correctly')", onmouseover="return validate(terms)"/>

How would I change it to also check customersaddress, customerstelephone, etc?

Thanks

Simon
 
ideally you'd throw it all into a function and call it from the function.

Code:
function verify() {
    return isEmpty(document.getElementById('customersname'), 'Please Fill in the form correctly') && isEmpty(document.getElementById('customersaddress'), 'Please Fill in the form correctly') && isEmpty(document.getElementById('customerstelephone'), 'Please Fill in the form correctly');
}

and call like

Code:
<input type="submit" name="submit" value="Continue to select you flight package" onclick="return verify();", onmouseover="return validate(terms)"/>



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Hey Simon, since you're new to the site I figured I'd point out some of the "good behavior" policies for tek-tips. When someone gives you a solution for your problem it's general practice to click the "thank ***** for this valuable post" link in their post. This will give them the little purple stars you see on the posts - and it also is what determines the MVP rankings per forum and site. You can give a star to someone for any reason (even if you didn't start the thread), and you can give stars to multiple people in a particular thread. So, since cory and Dianecht both helped you - you should give them both a star.

Your profile will show how many stars you give to other members as well as how many threads you've started. So, if you start a bunch of threads and never award stars it's gonna look like you never say "thanks" for the help you get when someone looks at your profile (and as a result you may get less help that way)

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top