1

I have this code in my view

function Validate() {
   if (document.getElementById('MandateName').value == "") {

       var err = document.getElementById('MandateNameErr');
       err.innerHTML = "Please enter a value for the Mandate Name";
       err.style.display = "block";
       return false;
   }
   else {
       document.getElementById('MandateNameErr').style.display = "none";
   }

   if (document.getElementById('MandateDescription').value == "") {
       var err = document.getElementById('MandateDescriptionErr');
       err.innerHTML = "Please enter a value for the Mandate Description";
       err.style.display = "block";
       return false;
   }
   else {
       document.getElementById('MandateDescriptionErr').style.display = "none";
   }

   return true;
}

and I have on submit button I am validating before submiting?

<button name="Submit" onclick="Validate()" >Add Variables to Mandate</button>

I called Validate function but its showing me if I am not entering anything on the text box if I click Button its showing me my validation message but same time its going to my view and throwing me the message?

even I put the return false; its not working is that something I am doing wrong?

1
  • 1
    Why did you remove the code? The question doesn't make nearly as much sense now... Commented Oct 7, 2010 at 15:53

1 Answer 1

3

You need to put return in the onclick, like this:

<button name="Submit" onclick="return Validate()" >Add Variables to Mandate</button>

Otherwise you're executing the validation...but not really caring about the result.

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

4 Comments

@Nick......forgetting the return, is the most common mistake in calling js functions
The most common mistake? How about a very common mistake in a deprecated style of attaching events? If you separate your script from the html, this won't happen.
@Juan - While I agree, I find fault with your argument, $("button").click(function() { Validate(); }); would have the exact same issue, it would need to be $("button").click(function() { return Validate(); }); or $("button").click(Validate);
I see... I guess returning false to prevent the default action is what should be deprecated, in favor of preventDefault

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.