1

I have a login form that needs to be validated . It has a couple of buttons . I need to validate my form in such a way that if the user forgets values in the button then i flag an error . However on success i move to a new page to continue work . i have the following but validation is not happening .

JQ :

 <script type="text/javascript">
        $(function() {

            $('#txtEmpid').click(
                    {
                        rules: {
                            txtEmpid: {
                                required: true
                            }
                        },
                        errorPlacement: function(error, element) {
                            element.css('background', '#ffdddd');
                        }
                    });
                    $('#txtPassword').click(
                    {
                        rules: {
                            txtPassword: {
                                required: true
                            }
                        },
                        errorPlacement: function(error, element) {
                            element.css('background', '#ffdddd');
                        }
                    });
        });
    </script>

HTML form ( index.html):

  <FORM NAME="frmLogin" ACTION="Login.jsp" METHOD="post" >
        <fieldset>
            <div class="text-center">
                <label class="text-center" for="txtEmpid">Employee Id </label>
                <div class="text-center">
                    <input type="text" id="txtEmpid" name ="txtEmpid" placeholder="Employee Id">
                </div>
            </div>
            <div class="text-center">
                <label class="text-center" for="txtPassword">Password</label>
                <div class="text-center">
                    <input type="password" id="txtPassword" name ="txtPassword" placeholder="Password">
                </div>
            </div>
            <div class="text-center">
                <div class="text-center">
                    <button type="submit" class="btn btn-primary">Sign in</button>
                </div>
            </div>
        </FIELDSET>

The above it not working . I am sure my jquery plugins are correct as i am able to do a validation for the entire submit form by using .validate jquery method .

I want to do validate every button . If values are ok then i move to new page (Login.jsp) else i stay on index.html and flag the problem . How do i do it ? Whats my mistake ?

6
  • form is not closed....is that typo..? Commented Mar 20, 2013 at 11:09
  • no no man ! the form displays just fine :-) just that my buttons are not validated . if i input values correctly it goes to Login.jsp just fine . I think is a problem with basic sytax/rules . So help me out in that regard . I think i am making a dumb mistake . Commented Mar 20, 2013 at 11:11
  • well i have added answer have a look Commented Mar 20, 2013 at 11:26
  • rules and errorPlacement are options/methods only for the jQuery Validate plugin and must be placed inside .validate()... they can't be arbitrarily placed inside of a .click() handler or anyplace else outside of .validate(). Commented Mar 20, 2013 at 16:13
  • @Sparky Thanks for pointing it out . Yes i am using the .validate() function and do realise that these cant be placed inside a .click() function . By the way the above is incorrect code . I have accepted the correct answer . Commented Mar 20, 2013 at 17:27

3 Answers 3

2

You need to user .validate.

Give form a unique ID.

Demo

HTML

<FORM NAME="frmLogin" ACTION="Login.jsp" METHOD="post" id="FormID">
    <fieldset>
        <div class="text-center">
            <label class="text-center" for="txtEmpid">Employee Id </label>
            <div class="text-center">
                <input type="text" id="txtEmpid" name ="txtEmpid" placeholder="Employee Id">
            </div>
        </div>
        <div class="text-center">
            <label class="text-center" for="txtPassword">Password</label>
            <div class="text-center">
                <input type="password" id="txtPassword" name ="txtPassword" placeholder="Password">
            </div>
        </div>
        <div class="text-center">
            <div class="text-center">
                <button type="submit" class="btn btn-primary">Sign in</button>
            </div>
        </div>
    </FIELDSET>
</form>

jQuery

<script type="text/javascript">
    $(function()
    {
        $('#FormID').validate(
        {
            rules: {
                txtEmpid: {
                    required: true
                },
                txtPassword: {
                    required: true
                }
            },
            errorPlacement: function(error, element) {
                element.css('background', '#ffdddd');
            }
        });
    });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You applied rules if user clicks only.If user dont clicks on any text box those will not apply.

<FORM NAME="frmLogin" ACTION="Login.jsp" METHOD="post" id="formId">

and call a function on onsubmit

function submitFunction()
{

return  $('#formId').valid();

}

or

you can use simply

           <input type="text" id="txtEmpid" name ="txtEmpid" class="required" 
           placeholder="Employee Id">

                <input type="password" id="txtPassword" class="required"  
                name ="txtPassword" placeholder="Password">

2 Comments

Ok i realise that problem . so when i change .click to .validate it still doest work :(
@rockstar if you have any other problem please create a alink
1

if u r jquery plugin is correct mean this code ll help you...

<script type="text/javascript">
        $(function() {

            $('#formID').validate(
                    {
                        rules: {
                            txtEmpid: {
                                required: true
                            }
                        },
                        errorPlacement: function(error, element) {
                            element.css('background', '#ffdddd');
                        }
                    });

        });
    </script>

2 Comments

I am sure about my plugins as form validation worked fine . But changing .click() to .validate() also does not work :( Hmmm
Got it now ! thaks for ur support !

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.