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

Need help with onBlur.... but not sure what it's called 1

Status
Not open for further replies.

TonyU

Technical User
Feb 14, 2001
1,317
US
[tt]
<select name=&quot;Dropdown&quot;>
<option value=&quot;Search&quot;>Search</option>
<option value=&quot;Choice1&quot;>Choice1</option>
<option value=&quot;Choice2&quot;>Choice2</option>
<option value=&quot;Choice3&quot;>Choice3</option>
</select>

I need to learn how to either display a Warning message when Choice2 is selected and not allow the user to continue if OK is not selected or how to set focus on a text field when Choice2 is selected. Hope it makes sense.


-Tony
Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
This should give you something to work with:

<html>
<body>
<script language=&quot;JavaScript&quot;>
function warnMe(theMenu)
{
if (theMenu.value == &quot;2&quot;)
{
if (confirm(&quot;are you sure?&quot;))
{
//do whatever you want to do when user wants to continue
}
else
{
//do whatever you want to do when user does not want to continue
document.getElementById('txtBox').focus();
}
}

}
</script>
<form method=&quot;POST&quot; >
<p><select size=&quot;1&quot; name=&quot;dropMenu&quot; onchange=&quot;warnMe(this)&quot;>
<option value=&quot;1&quot;>Selection 1</option>
<option value=&quot;2&quot;>Selection 2</option>
<option value=&quot;3&quot;>Selection 3</option>
</select></p>
<p><input type=&quot;text&quot; name=&quot;txtBox&quot; id=&quot;txtBox&quot; size=&quot;20&quot;></p>
</form>
</body>
</html>
 
[tt]thanks, that work great.

If I may ask two more questions.

1. How do I insert hard-returns in the &quot;are you sure&quot; and
2. Is it possible to disable the submit button unless the txtBox is populated.



-Tony
Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
1. You'll have to be more specific on what you mean by hard-returns

2. In your form tag (in addition to all the usual stuff) add this:
onsubmit=&quot;return (document.getElementById('txtBox').value.length > 0)&quot;

Where 'txtBox' is the id you've assigned to the text box that you want to make sure has content.

If there are more form elements you would like to validate, I would suggest creating a function and having it return a boolean value based on success or failure. The call to it would replace the current code:

onsubmit = &quot;return validateForm(this)&quot;

Of course it's always nice to tell the user why you're not letting them submit the form, so having a function that contains the conditional test above and uses &quot;alert&quot; in the case where the form can't be submitted would be polite :)

Note this will not disable the button, the form just won't submit (same difference functionally, but not visually).
 
[tt]By hard-returns I mean

are
you
sure

and not
&quot;are you sure&quot;


-Tony
Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
It you wanted that, you would simply make the string:
confirm(&quot;Are\nYou\nSure&quot;)
 
[tt]Thanks again


-Tony
Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
[tt]
I noticed that when adding this to the form

onsubmit=&quot;return (document.getElementById('txtBox').value.length > 0)&quot;

and other than choice2 is selected I still could not submit.


-Tony
Delete * from brain Where MaxLevel = &quot;Full&quot; and reaction = &quot;Slow&quot; order by StartOver
 
So the situation is that when 1 or 3 is chosen the textbox CAN be empty, but if 2 is chosen it can't be empty?

If so, go with the separate function option:

function validate(theForm)
{
//we don't care what's in the txt box in either of these cases
if (theForm.menu.value == &quot;1&quot; ||
theForm.menu.value == &quot;3&quot;)
return true;

//our original statement applies
return(theForm.txtBox.value.length > 0)

//if you want the alert, replace the one &quot;return&quot; line with this:
//if (theForm.txtBox.value.length > 0)
// return true;

// if we're here the length is 0, not acceptable
// alert('You must put something in the text box');
// return false;
}
</script>

Your form tag would look something like this:
<form method=&quot;POST&quot;
onsubmit=&quot;return validate(this)&quot; id=&quot;theForm&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top