Srinath Mohan
Srinath Mohan

Reputation: 11

Prevent form submission in java script after displaying alert message

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

Answers (2)

cfontanet
cfontanet

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

Sridhar R
Sridhar R

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

Related Questions