Simple field validation.

by shriek

HTML

<ul id="checker">
    <li>First Name:-
        <input type="text" class='mandatory' value="" />
    </li>
    <li>Middle Name:-
        <input type="text" value="" />
    </li>
    <li>Last Name:-
        <input type="text" value="" />
    </li>
     <li>Email:-
        <input type="text" value="" class="email" />
    </li>
</ul>
<input id="validate" type="button" onClick="carePackValidate('#checker','.mandatory','.email')" value="Validate" />
<div id="someotherfields">
    Random: <input type="text" /><br/>
    Field: <input type="text" />
</div>

JavaScript

function carePackValidate(id,classId,emailId) {
    var id = id ||'', classId = classId || '', emailId = emailId || '';
    var errorFlag = true;
    $(id + " " + "input[type='text']" + classId).each(function () {
        if ($(this).val() == "") {
            errorFlag = false;
            return false;
        }
    });
    if (errorFlag) {
        if(validateEmail($(emailId).val())){
            alert("YESSSS");
            return true;
        }
        else{
            alert("WRONG EMAIL");
            return false;
        }
    } else {
        alert("NOOOOO");
    }
}
function validateEmail(emailId) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(emailId);
}