1

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.

8
  • 2
    Please show us your form code. Commented Oct 28, 2013 at 8:58
  • Show to javascript code for submitting the form Commented Oct 28, 2013 at 8:59
  • you're missing a semicolon at the end. Commented Oct 28, 2013 at 9:01
  • @Asryael , I shall submit my fomr code as an answer Commented Oct 28, 2013 at 9:05
  • @Saturnix where is the semicolon missing? Commented Oct 28, 2013 at 9:06

2 Answers 2

1

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"/>

Sign up to request clarification or add additional context in comments.

2 Comments

what makes you think OP can use jQuery?
there may be some error in your function thatz why it not working well. Check whether you are getting value in variable Name and also make sure that there is an element with id name.
1

@Sridhar R answer worked for me, with a little change, instead of 'onsubmit' I used 'onSubmit'

$('#formID').attr('onSubmit','return false');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.