Validation not working (only one item)

Posted on 2012-08-17
Last Modified: 2012-08-17
On the attached file - ALL items in the enclosed form are being validated properly except one (AMPM field) - It is a radio button and that is the only difference? The code is at lines 361 and 947.
Question by:edavo
    LVL 49

    Accepted Solution

    You are using this to get the value of the radio button

    var ampm = document.getElementById('ampm').value;

    Which is not going to work - also you have duplicated id's for the radio button so it is even more difficult.

    1. Give each of the radio button's a unique id
    2. Check the value of the radio button's to see which is checked like so

    if (document.getElementById('ampm1').checked)
      ampm = document.getElementById('ampm1').value;
    else if (document.getElementById('ampm2').checked)
      ampm = document.getElementById('ampm2').value;

    Open in new window

    You might want to consider using JQuery as well - it makes this sort of code a lot easier.
    LVL 25

    Assisted Solution

    by:Kyle Hamilton
    first of all, you cannot have twoelements with the same id, namely ampm. your js will wig out for that reason alone.

    second, you're checking for the wrong value - the values of your radio buttons are "AM" and "PM", not "Ex:3:30"

    third, dont check for the value, but for wether a radio is selected or not.

    thats a start. if you need more help, just let us know.


    Author Comment

    best solution:

    else if(ampm3 == '' && ampm4 == ''){

                   alert("Please Enter Your AM/PM");
                      document.getElementById('ampm3' || 'ampm4').focus();

                   return false;


    Featured Post

    Course: Create Mobile App Prototypes with Adobe XD

    This is a project-based course: we go through all the steps of creating a prototype from start to finish, using all the tools and features currently available in Adobe XD. You can complete the course in less than a day, plus all project files and fonts are included.

    Join & Write a Comment

    SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
    The viewer will learn how to count occurrences of each item in an array.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now