Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this jQuery validation be refactored?
    primarykey
    data
    text
    <p>I'm doing some simple client side validation with jQuery.</p> <pre><code>var passedValidation = new Boolean(true); // Required Field Validators. if ($('#fbsignup input.firstName').val().trim() == '') { $('#fbsignup tr.firstName em').show(); passedValidation = false; } if ($('#fbsignup input.lastName').val().trim() == '') { $('#fbsignup tr.lastName em').show(); passedValidation = false; } if ($('#fbsignup input.email').val().trim() == '') { $('#fbsignup tr.email em').show(); passedValidation = false; } if ($('#fbsignup input.password').val().trim() == '') { $('#fbsignup tr.password em').show(); passedValidation = false; } if ($('#fbsignup input.screenName').val().trim() == '') { $('#fbsignup tr.screenName em').show(); passedValidation = false; } if (passedValidation == true) { // All validation passed. Hide the modal signup dialog and post back to signup user. $('#fbcSignupModal').jqmHide(); return true; } else { return false; } </code></pre> <p>Essentially, i want to ensure all fields are filled in. If any aren't, return false, else return true. Simple.</p> <p>Can this be refactored to a single line? (perhaps by applying a class to all elements?).</p> <p>A caveat on the answer, i do NOT want to use the jquery-validate plugin. I know its awesome, but this is not very difficult validation and i do not want to affect the rest of the form (this is a modal popup).</p> <p>So, that being said - any ideas?</p> <p><strong>EDIT</strong></p> <p>Just to clarify, i do need to know <em>which</em> field wan't filled in, so i can show an * next to it.</p> <p><strong>EDIT2</strong></p> <p>Updated the original code to indicate i need to show a required field label and return false if validation fails.</p> <p><strong>EDIT3</strong></p> <p>Okay i've rethought my solution, and it looks like i'll need to do a server-call to validate the email against the membership schema. So i'm probably going to end up either wrapping the fields in an update panel or doing a web service post (and return errors in a json array). However, i'll leave this question open for a while and pick the answer with the most votes.</p> <p><strong>ANSWER</strong></p> <p>So i've gone with a modified version of @box9's answer. I'll still need to do an AJAX call to the server to validate the email (as my edit above suggests), but this will ensure i only do that if all fields are filled in. </p> <pre><code>$('#fbsignup input.required').each(function (index) { if ($(this).val().trim() == '') { $(this).next('em').show(); passedValidation = false; } }); </code></pre> <p>I have an <em>em</em> element directly after the input fields which are required, so i can easily use the <code>.next([selector])</code> jQuery selector.</p> <p>Nice and easy.</p> <p>Thanks for all the answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload