Reputation: 11
This is my coding in js
var ck_name = /^[A-Za-z0-9 ]{3,12}$/;
function validate(form)
{
var Name = document.getquote.name.value;
if (!ck_name.test(Name))
{
alert("Enter a valid FirstName containing alphabets ,numbers with minimum of 3 characters");
document.getElementById('name').focus();
return false;
}
}
Iam calling this function on form submit. After showing the alert message, I want the focus to be back on the name-textbox but the page get submitted after the alert. The "return false" command is not working.
Upvotes: 1
Views: 2576
Reputation: 194
@Sridhar R answer worked for me, with a little change, instead of 'onsubmit' I used 'onSubmit'
$('#formID').attr('onSubmit','return false');
Upvotes: 1
Reputation: 20418
You add this code when false occurs
$('#formID').attr('onsubmit','return false');
Another Way
$("form").submit(function () { return false; });
that will prevent the button from submitting or you can just change the button type to "button" <input type="button"/>
instead of <input type="submit"/>
Upvotes: 1