0

I written an application that allows users to enter their personal and payment details.

I have a number of validation checks on the majority of the fields to ensure they are not blank and hope in the future to introduce other checks (e.g. numeric values only, no special characters)..

I look at my jQuery code, and it just seems i have gone about it in the long-winded way, using if else's. Here's my jQuery code using a sample of fields:

$(document).ready(function () {

$(".submitForm").click(function () {

    if ($("#Firstname").val() == '') {
        $("#Firstname").addClass("highlight");
    } else {
        $("#Firstname").removeClass("highlight");
    }

    if ($("#Surname").val() == '') {
        $("#Surname").addClass("highlight");
    } else {
        $("#Surname").removeClass("highlight");
    }

    if ($("#email").val() == '') {
        $("#email").addClass("highlight");
    } else {
        $("#email").removeClass("highlight");
    }

    if ($("#confirmemail").val() == '') {
        $("#confirmemail").addClass("highlight");
    } else {
        $("#confirmemail").removeClass("highlight");
    }

    ......
    ......

    $("#PersonalDetailsRequired").removeClass("visuallyhidden ");
    //document.forms[0].submit();
});
});

Is there a more efficient way of writing this?

Here's my jFiddle: http://jsfiddle.net/oampz/G3ccb/5/

Also, PersonalDetailsRequired seems to always be appear even if ALL the fields have a value in them.

Thanks

UPDATE:

Uploaded a new fiddle here - http://jsfiddle.net/oampz/P9eKZ/1/

What i'm trying to do is for the #PersonalDetailsRequired and #AddressDetailsRequired to only show if their respective fields are not filled in..

Thanks

4 Answers 4

1

Try this code:

DEMO

$(document).ready(function () {
    $(".submitForm").click(function () {
        $("input").each(function(){
            if($(this).val() == '')
            $(this).addClass("highlight");
            else 
            $(this).removeClass("highlight");       
        $("#PersonalDetailsRequired").removeClass("visuallyhidden ");
        //document.forms[0].submit();
        });
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, this makes sense for validating every field on my page.. But there are some fields which don't require validation and can be left blank.
Also, PersonalDetailsRequired seems to always be appear even if ALL the fields have a value in them.
validation of the particular fields, use class name ...try jsfiddle.net/manojmcet/X6nTq/1 ...here surname not validate.
Thanks! Uploaded a new fiddle here - jsfiddle.net/oampz/P9eKZ/1 What i'm trying to do is for the #PersonalDetailsRequired and #AddressDetailsRequired to only show if their respective fields are not filled in..
Thanks, i can understand what you have done creating new CSS classes but i was under the impression we should reuse classes wherever possible. See new fiddle: jsfiddle.net/oampz/P9eKZ/4
1

You can use each method:

$('form input').each(function(){
  if($(this).val()==''){
    $(this).addClass('highlight');
  } else {
    $(this).removeClass('highlight');
  }
});

As per your comment you can use not selector like this:

$(this).not('your_selector_not_to_validate').val()==''

2 Comments

thanks, this makes sense for validating every field on my page.. But there are some fields which dont require validation and can be left blank.
Thanks! Uploaded a new fiddle here - jsfiddle.net/oampz/P9eKZ/1 What i'm trying to do is for the #PersonalDetailsRequired and #AddressDetailsRequired to only show if their respective fields are not filled in..
1

For example, you can use toggleClass method in jquery, instead of if/else; and each to walk through form elements like:

$('form input.MyCoolSelectoForValidation').each(function(){
    $(this).toggleClass('highlight', ($(this).val()==''));
});

demo: http://jsfiddle.net/G3ccb/7/

UPD: and same way you can apply to your code for displaying error msg:

$("#PersonalDetailsRequired").toggleClass('visuallyhidden', $('.highlight').length < 1);

2 Comments

thanks, this makes sense for validating every field on my page.. But there are some fields which don't require validation and can be left blank.
than change selector to more specific
0

The efficient way will be to use HTML5 validation, need not to add script for this:

Here are three pragmatic (but not globally perfect) examples:

Strong password:

input title="at least eight symbols containing at least one number, one lower, and one upper letter" type="text" pattern="(?=.\d)(?=.[a-z])(?=.*[A-Z]).{8,}" required

Email address:

input type="text" title="email" required pattern="[^@]+@[^@]+.[a-zA-Z]{2,6}"

Phone number:

input type="text" required pattern="(+?\d[- .]*){7,13}" title="international, national or local phone number"

1 Comment

this makes no sense - do you have a working example?

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.