0

I am having an input field and a submit button:

        <form action="" method="post">
           <input type="text" name="sponsar_name" id="sponsar_name" />
          <input type="submit" value="Submit"  class="btn-submit" name="submit" onClick="validate()" >
    </form>

      function validate()
    {
        var flag = 0;
    if(document.getElementById("sponsar_name").value == null || document.getElementById("sponsar_name").value == "")
        {
            document.getElementById("sponsar_name").style.borderColor = 'red';
            flag = 1;
            }
            else
            {
                        document.getElementById("sponsar_name").style.borderColor = '';
                }

                if(flag==1)
                {
                    return false;
                }
                else
                {
                    return true;
                }

    }
                     </script>

I have applied a function onClick on submit button .this I have applied for validation.

When I click on submit button it shows red background of textbox but refreshes the page immediately.

If the textbox is blank it should not refresh the page, but in my code it is doing so.

Can anyone help me?

2
  • 4
    onClick="return validate()" does the trick. Commented Jun 18, 2014 at 5:53
  • top comment is right. and you have good code. the first sugestion creating varibale for document.getElementById("sponsar_name"). Commented Jun 18, 2014 at 5:55

4 Answers 4

1

Try with

<form action="" method="post">
   <input type="text" name="sponsar_name" id="sponsar_name" />
   <input type="submit" value="Submit"  class="btn-submit" name="submit" onClick="return validate()" >
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use either onClick="return validate()" or onSubmit="return validate()". It will fix the problem.

using onClick="return validate()"

<form action="" method="post">
    <input type="text" name="sponsar_name" id="sponsar_name" />
    <input type="submit" value="Submit"  class="btn-submit" name="submit" onClick="return validate()" >
</form>

using onSubmit="return validate()"

<form action="" method="post" onSubmit="return validate()">
    <input type="text" name="sponsar_name" id="sponsar_name" />
    <input type="submit" value="Submit"  class="btn-submit" name="submit">
</form>

Comments

0

You need to do it this way,

<input type="submit" value="Submit" class="btn-submit" name="submit" onClick="return validate()" >

And when you will return false in your method, your form won't be submitted.

Comments

0

Change you form like this: remove onClik function and add the onSubmit on form header

<form action="" method="post" onsubmit="return validate()">
           <input type="text" name="sponsar_name" id="sponsar_name" />
          <input type="submit" value="Submit"  class="btn-submit" name="submit"/> 
    </form>

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.