1

I am using jquery form validation to alert the user if the form is not valid.

if (!$(Form1).valid()) {
    alert("invalid form")
}

is there a way to force the form to be invalid in javascript.

for example I have a textbox and if it's value is "123" I want the form to become invalid.

something like this:

if($("#TextBox1").val()=="123"){
    $(Form1).valid() = false;
}
Y2theZ
  • 10,162
  • 38
  • 131
  • 200
  • 1
    I'm not aware that you can do that... can't you just create the necessary rule to validate if the textbox has that specific value? Do you know you can create custom rules? – Claudio Redi May 08 '12 at 12:24

3 Answers3

1

you can set the value (true or false) to a variable

if($("#TextBox1").val()=="123"){
    var responce1 = false;
}
if($("#TextBox2").val()=="abc"){
    var responce2 = false;
}

and at last you can check as

 var resultresponce=responce2 && responce1;

and lastly you need to check if the value of resultresponce is true... if so you can submit the form or else not..

if(resultresponce)
{
$(form).submit();
}
Philemon philip Kunjumon
  • 1,417
  • 1
  • 15
  • 34
0

You should try to just use a if statement for this one. Something like:

if ($("#el").val() == "123") {
    // Do something or nothing here.

} else if (!$(Form1).valid()) {
    alert("invalid form")

} else {
    // All correct

}

What philnate says should also work fine. It's more what you want to happen.

Wouter Konecny
  • 3,260
  • 3
  • 19
  • 26
0

What you can do to invalidate the Form is to disable the connected submit button, so the user can't submit it. See: jQuery disable/enable submit button

Community
  • 1
  • 1
philnate
  • 1,506
  • 2
  • 21
  • 39