-
onBlur
on my html page, I have one text box and one submit button.Text box has validation routine on its onBlur event. When I enter invalid text in text button and try to click submit button validation routine works fine and alert message is shown and form is not submitted. Submit button can also be called by Alt + G (I have set up code for hotkeys using javascript), but this time onBlur event is not invoked as focus remains on the text box.
how can I stop form from being submitted when user presses Alt+G with invalid text in text box. The page may contain multiple text boxes with onBlur events.
-
hi!
Insted of using a submit button you can use a normalbutton and
onthe mousedown event you call the validate function and
you also call this function from the Alt+G press
function validate()
{
valid = isvalid()
if(valid == true){
document.form.submit
}
}
-
I would do it a little differently, and use the onsubmit event handler of the form. This way you don't need to bother manually calling the function, and you only need to declare it once, and it will work regardless of whether the user pressed enter, clicked the button or whatever:
Code:
<script type="text/javascript"><!--
function validate(formElement) {
//ALL YOUR VALIDATE STUFF HERE -
// RETURN TRUE IF ALL IS OK
if (formElement.txt.value != '') {
return true;
}
//Only gets here if the form doesn't validate
return false;
}
//--></script>
<form action="page.php"
onsubmit="return validate(this);"><p>
<input name="txt" /><input accesskey="G" type="submit" />
</p></form>