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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple problem I'm sure

Status
Not open for further replies.

doorbreaker

Programmer
Nov 19, 2002
91
GB
Hi,

How do I put an else statement in??

I want to check if a field is blank, if it is then ask the user to fill it in.

If it is filled in then ask them to check that it is correct...

Thanks

Chris

Code Below:


if (document.frm.value=='')
{
alert ('Please enter all personalisation text');
document.frm.focus();
return false;
}

var answer = confirm ("Is the personalisation text for your order correct?\n\nIf YES, then please click OK to proceed to our Secure Server\n\nIf NO, then please click CANCEL to re-enter your text")
if (answer)
document.frm.action='order_ends.asp'
else
return false;
 

What you have looks good. Does it not work for you?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Sorry, maybe I should include more of the code as not all of the products require this text field to be filled out (at present it wrongly asks them to check the text when no text is required)...

Code:
function checkout()
{
for (var i=1;i<document.frm.length;i++)
if (document.frm.name.substr(0,4)=='Pers')
if (document.frm.value=='')
{
alert ('Please enter all personalisation text');
document.frm.focus();
return false;
}

var answer = confirm ("Is the personalisation text for your order correct?\n\nIf YES, then please click OK to proceed to our Secure Server\n\nIf NO, then please click CANCEL to re-enter your text")
if (answer)
document.frm.action='order_ends.asp'
else
return false;

document.frm.action='order_ends.asp'
// -->
}

I need it so that if 'Pers' is present, it checks if the field is filled out, if it is filled out, it asks the user to verify the field.

If 'Pers' is not present it should go straight on to order_ends.asp.
 
I did it!

After putting in the else statement - sorry for wasting anyone's time

Chris

function checkout()

{
for (var i=1;i<document.frm.length;i++)
if (document.frm.name.substr(0,4)=='Pers')
if (document.frm.value=='')
{
alert ('Please enter all personalisation text');
document.frm.focus();
return false;
}
else
{
var answer = confirm ("Is the personalisation text for your order correct?\n\nIf YES, then please click OK to proceed to our Secure Server\n\nIf NO, then please click CANCEL to re-enter your text")
if (answer)
document.frm.action='order_ends.asp'
else
return false;
}

document.frm.action='order_ends.asp'
// -->
}
 
You can also compact the syntax somewhat. I like this form better:
Code:
if ( cond ) {
  ...
} else {
 ...
}
if the if statement is very long, I also add a comment on the closing brace which repeats the condition, so I'll know what condition the brace is closing.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top