leddy
leddy

Reputation: 553

jquery validate - multiple forms and email validation

I have on my page two form tags - one of them is within a bootstrap modal. I want to validate them independantly of each other.

The first form frmPO works fine. The second form frmEmail (click on the Print/Email Purchase Order button) - when i check if it's valid, it always returns true. I want Email From and Email To to be both required and email: true.

I've shortened my page down here best I could to show the issue JSFiddle

HTML for frmPOPrint

    <form id="frmEmail" action="" method="post">
    <div class="modal fade printModal" id="printPOModal" tabindex="-1" role="dialog" aria-labelledby="printPOModal" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-body" id="printModalBody">
                    <iframe id="ifrPrint" src="Default.htm" style="width: 99.6%; height: 85%"></iframe>
                    <div class="row">
                        <div class="col-sm-4">
                            <label for="txtEmailFromPOPrint">Email From:</label>
                            <input class="form-control" type="text" id="txtEmailFromPOPrint" value="[email protected]"/>
                        </div>
                        <div class="col-sm-4">
                            <label for="txtEmailToPOPrint">Email To:</label>
                            <input class="form-control" type="text" id="txtEmailToPOPrint" />
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button id="btnEmail" type="button" class="btn btn-primary">Email</button>
                </div>
            </div>
        </div>
    </div>
</form>

JS

jQuery.validator.setDefaults({
    debug: true,
    success: "valid"
});

$("#frmEmail").validate({
    rules: {
        txtEmailFromPOPrint: {
            required: true,
            email: true
        },
        txtEmailToPOPrint: {
            required: true,
            email: true
        }
    }
});

$('#btnEmail').click(function () {

    if ($("#frmEmail").valid()) {
        alert("valid!");
    }
});

Any help would be greatly appreciated

Upvotes: 0

Views: 437

Answers (1)

Sparky
Sparky

Reputation: 98738

It's because your input elements have no name attributes. The rules declared inside the rules object within the .validate() method can only be assigned to the inputs' name attributes.

<input type="text" id="txtEmailFromPOPrint" name="txtEmailFromPOPrint" ... />
<input type="text" id="txtEmailToPOPrint" name="txtEmailToPOPrint" ... />

DEMO: http://jsfiddle.net/pb52uc0h/2/

$("#frmEmail").validate({
    rules: {
        txtEmailFromPOPrint: { // <- NAME attribute
            required: true,
            email: true
        },
        txtEmailToPOPrint: { // <- NAME attribute
            required: true,
            email: true
        } ....

Upvotes: 2

Related Questions