SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict Feudalism's Avatar
    Join Date
    Jun 2002
    Location
    Buenos Aires, Argentina
    Posts
    361
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question select input validator

    While this code works allright for text-inputs validation, It doesnt work for "select" types.

    Code:
        if (document.nueva.pricing.value == '' ) {
                                    alert('El campo `pricing´ es obligatorio.');
                                    nueva.pricing.focus();
                                    error_de_validacion = 'S';
                                    return(false);
                                }

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When accessing elements with an ID its best using document.getElementById

    ie

    Code:
    if (document.getElementById('pricing').value == '' ) {
        alert('El campo `pricing´ es obligatorio.');
        nueva.pricing.focus();
        error_de_validacion = 'S';
        return(false);
    }
    Also, with select elements use the selectedIndex value to check.

    I tend to add a 'Please Select' option, and check if that has or hasn't been selected, Usually the selectedIndex should be more than 0 (if you have the please select option)



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •