0

I have a form like that:

 <script type="text/javascript">
    function checkpas() 
    {
        var newp = $('#NewPassword').val();
        var newpp = $('#RepeatedNewPassword').val();

        if (newp != newpp) 
        {
            return false;
        }
    }
  </script>

<% using (Html.BeginForm()) {%>
    <%: Html.ValidationSummary(true) %>
         ...
    <input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />
<% } %>

but that submit input always invokes a form submit, even if I return false in the JS code. Why ?

2 Answers 2

2

you need to run onsubmit() not onclick()

Like this:

 <script type="text/javascript">
    $("#yourformID").submit(function (evt)
    {
        var newp = $('#NewPassword').val();
        var newpp = $('#RepeatedNewPassword').val();

        if (newp != newpp) 
        {
            return false;
        }
    }
  </script>

Note assign a form ID by doing this in your ASPX. here is a reference http://msdn.microsoft.com/en-us/library/dd492714.aspx

<% using (Html.BeginForm("ActionName", "ControllerName", Method, new {id="myFormID")})) {%>
Sign up to request clarification or add additional context in comments.

Comments

1

I believe you need to return the function result: instead of:

<input type="submit" id="sub_btn" value="Edit" onclick="checkpas();" />

use:

<input type="submit" id="sub_btn" value="Edit" onclick="return checkpas();" />

also in your function add an else return true;

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.