-2

I am trying to make a javascript validating form, and am a bit stuck on validating drop down inputs (select)

I have been using this so far but am unsure on how to implement the validation to the select options, if anyone could give me some tips that would be great.

Edit: Also, how would I implement email validation, e.g containing @, thanks

Thanks

<input id="firstname" onblur="validate('firstname')"></input>

Please enter your first name Thanks

http://jsfiddle.net/ww2grozz/13/

Dannad
  • 151
  • 1
  • 10

3 Answers3

0

you need to handle select as follow

var validated = {};

function validate(field) {
    // Get the  value of the input field being submitted

    value = document.getElementById(field).value;

    // Set the error field tag in the html
    errorField = field + 'Error';

    // Set the success field
    successField = field + 'Success';

    if (value != '') {
        document.getElementById(successField).style.display = 'block';
        document.getElementById(errorField).style.display = 'none';
        validated[field] = true;
    } else {
        document.getElementById(successField).style.display = 'none';
        document.getElementById(errorField).style.display = 'block';
        validated[field] = false;
    }
}

function SimulateSubmit() {
    // Query your elements
    var inputs = document.getElementsByTagName('input');

    // Loop your elements
    for (i = 0, len = inputs.length; i < len; i++) {
        var name = inputs[i].id;

        if (!validated[name]) {
            // Call validate
            validate(name);
            // Prevent default
        }
    }


    var all_select = document.getElementsByTagName("select"); // get al select box from the dom to validate
     for (i = 0, len = all_select.length; i < len; i++) {
        var name = all_select[i].id;

        if (!validated[name]) {
            // Call validate
            validate(name);
            // Prevent default
        }
    }

}

here the Working fiddle

Shailendra Sharma
  • 6,976
  • 2
  • 28
  • 48
0

using jQuery function

$('input').on('keyup', function() {
  var isValid = $.trim($(this).val()) ? true : false;
  // show result field is Valid
});
Dongin Min
  • 409
  • 3
  • 5
  • 12
0

You must use <form> tag and set your action to it I have done that check this link and I have added select tag and set it to -1 by default for checking purpose while validating

Domain
  • 11,562
  • 3
  • 23
  • 44