Jump to content

Java Alert


mcmuney

Recommended Posts

try this:

 

<script language="javascript">
function checkZIP() 
{
var zippy = document.getElementById('zipcode').value.length;
if (zippy < "5") {
alert("Please Enter A Valid Zip Code");
}
else {
document.zipform.submit();
}
}
</script>

<form name="zipform" action="gasfinder.php" method="post">
Zip Code: <input type="text" name="zip" id="zipcode" maxlength="5">
<input type="button" value="Get Gas Now!" onclick="checkZIP()">
</form>

Link to comment
Share on other sites

 

do this then:

 

<script language="javascript">
function checkZIP() 
{
var zippy = document.getElementById('zipcode').value.length;
if (zippy < "5") {
alert("Please Enter A Valid Zip Code");
}
else {
document.zipform.submit();
}
}
</script>

<form name="zipform" action="gasfinder.php" method="post" onsubmit="checkZIP(); return false">
Zip Code: <input type="text" name="zip" id="zipcode" maxlength="5">
<input type="button" value="Get Gas Now!" onclick="checkZIP()">
</form>

Link to comment
Share on other sites

I don't think you're understanding the issue. The alerts are working fine when the button is clicked. The alerts are not working when the ENTER key is pressed. On ENTER, the page just refreshes, it only works when the button is clicked. So, I want an alert to display when ENTER key is pressed, to say, "Please click the search button".

Link to comment
Share on other sites

I don't think you're understanding the issue. The alerts are working fine when the button is clicked. The alerts are not working when the ENTER key is pressed. On ENTER, the page just refreshes, it only works when the button is clicked. So, I want an alert to display when ENTER key is pressed, to say, "Please click the search button".

 

and the code I give you above does just that.

Link to comment
Share on other sites

Hmmm, then maybe I'm not doing it right. Here's my original code if that helps:

 

<!-- Begin
function dosearch(form) {
var zipcode = form.zipcode.value;
var valid	= "0123456789"
if (! zipcode) {
alert("Please enter a Zipcode!");
form.focus();
form.select();
}
if (zipcode.length > 5) {
alert("Invalid Zipcode! Must be 5 characters!");
form.focus();
form.select();
}
if (zipcode.length < 5) {
alert("Invalid Zipcode! Must be 5 characters!");
form.focus();
form.select();
}
var ok		= 1;
var temp;
for (var i=0; i < zipcode.length; i++) {
temp = "" + zipcode.substring(i, i + 1);
if (valid.indexOf(temp) == "-1") { ok = 0; }
}
if (ok == 0) {
alert("Invalid Zipcode! Only number allowed!");
form.focus();
form.select();
}
else { window.location = 'zipcode_' + zipcode; }
}
function openMap(url, name) {
var temp	= "";
url			= '' + url;
splitstring	= url.split(" ");
for(i = 0; i < splitstring.length; i++) {
if (i > 0) { temp = temp + '_' + splitstring[i]; }
else { temp = splitstring[i]; }
}
url = temp;
window.open(url, name, 'scrollbars=1,resizable=no,width=550,height=450,status=0,menubar=0');
}
//  End -->

 

And the form:

 

<form action="index.php" method="get" name="search">
<table border="0" cellpadding="3" cellspacing="2">
<tr>
	<td align="right">Zipcode: <input name="zipcode" type="text" class="textbox" value="<?=$zipcode;?>" size="8" maxlength="5"/></td>
	<td><input type="button" value="Click to Get Gas Prices" onclick="dosearch(this.form);"/></td>
</tr>
</table>
</form>

Link to comment
Share on other sites

 

your not adding the onsubmit() event to the form tag.

 

onsubmit="checkZIP(); return false"

 

checkZIP()?

 

If you want to catch the "enter" event, just do what he said, but the form will look like this:

 

<form action="index.php" method="get" name="search" onsubmit="dosearch(this);return false;">

Link to comment
Share on other sites

I added your full script, in addition to mine, then added the onsubmit code along with the onclick. I did this because without onclick, the search function doesn't work. Anyhow, none of my attempts have been successful. On ENTER the page always refreshes. Here's what the last one looked like:

 

I added onsubmit="checkZIP(); return false" and the id="zipcode" to my existing code

<form action="index.php" method="get" name="search">
<table border="0" cellpadding="3" cellspacing="2">
<tr>
	<td align="right">Zipcode: <input name="zipcode" id="zipcode" type="text" class="textbox" value="<?=$zipcode;?>" size="8" maxlength="5"/></td>
	<td><input type="button" value="Click to Get Gas Prices" onclick="dosearch(this.form);" onsubmit="checkZIP(); return false"/></td>
</tr>
</table>
</form>

 

 

And I'm utilizing this in addition to the existing script:

 

<script language="javascript">
function checkZIP() 
{
var zippy = document.getElementById('zipcode').value.length;
if (zippy < "5") {
alert("Please Enter A Valid Zip Code");
}
else {
document.search.submit();
}
}
</script>

Link to comment
Share on other sites

 

you still didn't do it like I was telling you to; do it like this:

 

<script language="javascript">
function checkZIP() 
{
var zippy = document.getElementById('zipcode').value.length;
if (zippy < "5") {
alert("Please Enter A Valid Zip Code");
}
else {
document.search.submit();
}
}
</script>

<form action="index.php" method="get" name="search" onsubmit="checkZIP(); return false">
<table border="0" cellpadding="3" cellspacing="2">
<tr>
	<td align="right">Zipcode: <input name="zipcode" id="zipcode" type="text" class="textbox" size="8" maxlength="5"/></td>
	<td><input type="button" value="Click to Get Gas Prices" onclick="checkZIP();dosearch(this.form)"/></td>
</tr>
</table>
</form>

Link to comment
Share on other sites

Ok, I used the exact code.

 

RESULT: It now alerts on ENTER. This is what I was looking for but this also helps.

 

Problem: On valid zipcode entry, ENTER just refreshes the page (see what I'm talking about by trying it here:  http://www.gasdays.com) Try pressing ENTER after placing a valid zipcode. This is where I want to utilize the alert to say "Click the button".

Link to comment
Share on other sites

 

Your form action is for "index.php"; of course it is going to submit back to itself (the domain follows the index page); unless you have another index file, with a different file extension (ie "html" or "htm"). If your "index.php" is supposed to redirect; then you might want to take a look at your php code for this issue.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.