Jump to content

jQuery - need alert when empty form submitted


cyber-guys
 Share

Recommended Posts

I have a link that opens a small form asking for an account code - when the user enters a valid account code it opens another page whose url is mysite.com/'account code'/menu.htm The folder named 'account code' is a protected folder and the user will then log in with a userid/passwordIf the user does not enter anything I would like an alert 'Please enter something in the box" and if they enter a wrong code an alert that says 'Wrong code, please try again!'upon opening the menu page, the account code form closes itselfThe problem I'm having is with the nothing entered alert - if nothing is entered the redirect goes to mysite.com/menu.htm which gives a 404 and proceeds to close the form - entering a wrong code gives the correct alert and keeps the form open like I want it to. This is the code in the form:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD><TITLE>Enter Account Code</TITLE><META content="text/html; charset=UTF-8" http-equiv=Content-Type><META name=GENERATOR content="MSHTML 8.00.6001.19046"><STYLE type=text/css>BODY { FONT-FAMILY: verdana, arial, sans-serif}</STYLE><div id="some_div" style="display:none"></div><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script><script type="text/javascript">$(document).ready(function(){ $("#some_div").ajaxComplete(function(e, xhr) { if(xhr.status == '404'){ alert("Wrong code, please try again!"); }else{ open_window(); } }); });function send_to_url(){ $('#some_div').load($("#input").val()+"/index.htm"); if(input_field == ''){ alert("Please enter something in the box!"); return false }}function open_window(){ window.open($("#input").val()+"/menu.htm"); window.close();}</script><div id="some_div" style="display:none"></div><Font face="verdana,arial" size=2 color="#000000"><center>Please Enter Account Code<BR><input type="text" name="input" id="input"><br><input type="button" name="buttton" value="Submit" onClick="send_to_url()"> </BODY></HTML>You can visit the page at http://www.windfieldalloy.com/login.htm - code=PartnerDemo, user=guest, pass=guestI'm sure the problem is syntax but I'm totally new at this jQuery/Ajax stuffThanks in advance for any help,Jim

Link to comment
Share on other sites

to check for an empty string, use ""I think it would be better to test the value(s) of the input element(s) for an empty string on submit, if it's not empty, then test the request by submitting the code or credentials, and if it comes back OK, then you can do one thing, else, do something else.

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...