0

What I'm trying to do here is have a form validation. I have 3 element here, a,b and c.

What I need to do is:

Edit: Just to correct my question

If a != 0 || a != '' validation_holder = 1;

else, if var a has value greater than zero, compare the value of b and c, c should not greater than b

if no errors submit the form.

I'm trying to do this in nested if else.

And here's what I have right now. http://jsfiddle.net/jWL9s/2

Any help will appreciate.

<form action="" method="post" id="register_form"  name="register_form">
PO<input type="text" id="a" value="0"/><br/>
Remain<input type="text" id="b" value="10"/><br/>
PR<input type="text" id="c" value="5"/><br/>
<span class="alert"></span>
<input name="edt_submit" type="submit" value="Edit">
</form>

jQuery(function($) {
var validation_holder;

$("form#register_form input[name='edt_submit']").click(function() {

var validation_holder = 0;

    var a           = $("form#register_form input[id='a']").val();
    var b           = $("form#register_form input[id='b']").val();
    var c           = $("form#register_form input[id='c']").val();

    if(a != 0 && a != '') {
        $("span.alert").html("You Cannot edit").addClass('validate');
        validation_holder = 1;
    } else {
    if( b > c ) {
        $("span.alert").html("Value is Greater than").addClass('validate');
        validation_holder = 1;
        } else {
        $("span.alert").html("");
        }
    }

    if(validation_holder == 1) { // if have a field is blank, return false
        $("p.validate_msg").slideDown("fast");
        return false;
    }  validation_holder = 0; // else return true
    /* validation End */    
}); // click End 

}); // jQuery End
7
  • if( c > b ) { is a string comparison. Commented Jul 30, 2014 at 2:31
  • You want an "AND" not an "OR" Commented Jul 30, 2014 at 2:33
  • Because if..else works differently in jQuery? Commented Jul 30, 2014 at 2:39
  • $("form#register_form input[id='a']") is hugely less efficient than: $('#a'). Commented Jul 30, 2014 at 2:41
  • @RobG Okay, so how I can do this? Commented Jul 30, 2014 at 2:44

2 Answers 2

1

By any chance are you looking for this solution (updated fiddle)

Markup:

<form id="register-form">
    <label for="a">PO</label>
    <input type="text" id="a" value="0"/>

    <label for="b">Remain</label>
    <input type="text" id="b" value="10"/>

    <label for="c">PR</label>
    <input type="text" id="c" value="5"/>

    <p id="message"></p>

    <input type="submit" value="Edit Summary">
</form>

JavaScript:

var validate = function(e) {
  e.stopPropagation();
  e.preventDefault();
  var a = $('#a').val();
  var b = parseInt($('#b').val());
  var c = parseInt($('#c').val());
  var message = $('#message');
  if(a === '' ||  parseInt(a) === 0) {
    message.html('You cannot edit.');
  } else {
    if( c > b ) {
      message.html('Value is Greater.');
    } else {
      message.html('');
      this.submit();
    }
  }
}
$('#register-form').on('submit', validate);
Sign up to request clarification or add additional context in comments.

4 Comments

(a === '' || parseInt(a) === 0) can be a == 0. And none of the form controls will be submitted since none have names.
@RobG You are correct but the intention of the code is to demo the logic what OP has been asking for.
@sarbbottam You may want to add a radix to the parseInt calls (for posterity)
@sarbbottam—it helps to answer issues in the OP even if a question isn't asked about them specifically. It saves the OP asking yet another question and "it doesn't work" responses.
0

Note that a, b and c are strings since form control values are strings.

If a == 0 or a =='' validate and show the message 'You cannot edit the QTY'

The test seems fine, as == will convert the string value of a to a number since the other value is a number per the Abstract Equality Comparison Algorithm, so if a is either zero, an empty string or a string of only white space, the following statement is executed.

If a != 0 || a != '' , compare the value of c > b validate and show the message 'Value is Greater than'

That seems fine, except you don't need the second test as it's already covered by a != 0 since an empty string or one containing only whitespace is converted to 0 per the algorithm above.

Note that form controls must have a name to be successful, an ID is suitable.

The expression: $("form#register_form input[id='a']").val() can be $('#a').val().

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.