+ Reply to Thread
Results 1 to 6 of 6

Thread: Javascript Help

  1. #1
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Question Javascript Help

    With the way this script is I've tried to add in radio button validation but im unsure of this. It's all under gender... the id of the radio buttons is gender and I semi familiar with
    if ( ( gender[0].checked == false ) && ( gender[1].checked == false ) ) { alert ( "Please choose your Gender: Male or Female" ); return false; }

    But I'm not sure how to include it in the way this script is set out. I tried but got lost... still learning, would appreciate any help. Would also like to know how to include a check box validation in this form. Thanks~!

    heres the form http://newdesigns.x10hosting.com/js_...t/loggedin.htm

    This js file; http://newdesigns.x10hosting.com/js_...t/js/script.js

    HTML Code:
    function Validate(){
        // Creating variable for field names
        var firstname = document.getElementById('firstname');
        var addr = document.getElementById('addr');
        var postcode = document.getElementById('postcode');
        var state = document.getElementById('state');
        var username = document.getElementById('username');
        var email = document.getElementById('email');
        var thedate = document.getElementById('txtDate');
        var gender = document.getElementById('gender');
        
        if(isAlphabet(firstname, "Please enter only letters for your name")){ // checks for alphabetical letters only
            if (checkGender(gender, "Please choose your Gender: Male or Female" )){        
                if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){ // check for both alphabetic and numeric characters only
                    if(isNumeric(postcode, "Please enter a valid postcode code")){ // check for numeric characters only
                        if(madeSelection(state, "Please Choose a State")){// checks if a selection is made
                            if(lengthRestriction(username, 6, 8)){ // checks the username is between 6 and 8 characters
                                if(emailValidator(email, "Please enter a valid email address")){ //checks if is a valid email address
                                    if(dateValidator(thedate,"Please enter valid date")){ // checks for valid date and correct format
                                    
                                    return true;
    
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }    
        return false;
        
    }
    
    // below are all the individual functions that test for valid input of each field in the forms
    
    //checks that an element is not empty, this isnt used anywhere that I can see
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
    
    //this one checks to make sure the inout is only numbers 0-9
    function isNumeric(elem, helperMsg){
        var numericExpression = /^[0-9]+$/;
        //alert(elem.value)
        if(elem.value.match(numericExpression)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    
    //this one tests for letters, either uppercase or lowercase and will also allow a space since full name is asked for
    function isAlphabet(elem, helperMsg){
        var alphaExp = /^[a-zA-Z\s]+$/;
        //alert(elem.value)
        if(elem.value.match(alphaExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    
    //this one is used to test for letters and numbers and is used for the address
    function isAlphanumeric(elem, helperMsg){
        //alert(elem.value)
        var alphaExp = /^[0-9a-zA-Z\s]+$/;
        if(elem.value.match(alphaExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
    
    //CHECKS IF USERNAME IS BETWEEN 6 - 8 CHARACTERS OTHERWISE WILL ALERT MESSAGE
    
    function lengthRestriction(elem, min, max){
        //alert(elem.value)
        var uInput = elem.value;
        if(uInput.length >= min && uInput.length <= max){
            return true;
        }else{
            alert("Please enter between " +min+ " and " +max+ " characters");
            elem.focus();
            return false;
        }
    }
    
    //CHECKS IF A SELECTION HAS BEEN MADE FROM THE DROPDOWN LIST, IF IS PLEASE CHOOSE, WILL ALERT MESSAGE
    function madeSelection(elem, helperMsg){
        //alert(elem.value)
        if(elem.value == "Please Choose"){
            alert(helperMsg);
            elem.focus();
            return false;
        }else{
            return true;
        }
    }
    
    //GENDER VALIDATION //////////////////////////////////////////////////////////
    function checkGender(elem, helperMsg){
        //alert(elem.value)
        if(gender[0].checked == false ) && (gender[1].checked == false){
            alert(helperMsg);
            elem.focus();
            return false;
        }else{
            return true;
        }
    }
    
    // CHECKS THAT A VALID EMAIL ADDRESS HAS BEEN FILLED IN
    function emailValidator(elem, helperMsg){
            //alert(elem.value)
        var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
        if(elem.value.match(emailExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }    
    
    //CHECKS THAT THE CORRECT DATE AND FORMAT IS FILLED IN
    function dateValidator(elem, helperMsg){
            //alert(elem.value)
        var dateExp = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/ ;
        if(elem.value.match(dateExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }

  2. #2
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Javascript Help

    I checked your html link above and your html does not have any reference to a field named "gender".
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  3. #3
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Re: Javascript Help

    Quote Originally Posted by quantum1 View Post
    I checked your html link above and your html does not have any reference to a field named "gender".
    I was doing it through my editor and hadn't put it up yet I realize. I'll upload now. I really need to get this figured out asap... quickly!!

    Here's you reference though
    HTML Code:
    var gender = document.getElementById('gender');
    HTML Code:
    if (checkGender(gender, "Please choose your Gender: Male or Female" )){	
    and

    HTML Code:
    //GENDER VALIDATION //////////////////////////////////////////////////////////
    function checkGender(elem, helperMsg){
        //alert(elem.value)
        if(gender[0].checked == false ) && (gender[1].checked == false){
            alert(helperMsg);
            elem.focus();
            return false;
        }else{
            return true;
        }
    }
    and the form

    HTML Code:
    <form name="form_valid" onsubmit='return Validate()' action="validgood.htm">
            <table class="text" cellpadding="5">
                
                <tr>
                    <td align="right">First Name:</td><td><input type='text' id='firstname' /></td>
                </tr>
                <tr>
                    <td align="right">Gender:</td>
                    <td>
                        Male<input type='radio' id='gender' value="male"/>
                        Female<input type='radio' id='gender' value="female"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">Address:</td><td><input type='text' id='addr' /></td>
                </tr>
                <tr>
                    <td align="right">State:</td><td>    <select id='state'>
                                                            <option value="Please Choose">Please Choose</option>
                                                            <option value="NSW">NSW</option>
                                                            <option value="QLD">QLD</option>
                                                            <option value="VIC">VIC</option>
                                                            <option value="SA">SA</option>
                                                            <option value="NT">NT</option>
                                                            <option value="WA">WA</option>
                                                            <option value="TAS">TAS</option>
                                                            <option value="ACT">ACT</option>
                                                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">Post Code:</td><td><input type='text' id='postcode' /></td>
                </tr>
                <tr>
                    <td align="right">Username:</td><td><input type='text' id='username' /></td>
                </tr>
                <tr>
                    <td align="right">Email:</td><td><input type='text' id='email' /></td>
                </tr>
                
                <tr>
                    <td align="right">Date:</td><td><input type="text" id="txtDate" maxlength="10"> (dd/mm/yyyy)</td>
                </tr>
                <tr>
                    <td align=""><input type="submit" value="Now Validate"></td><td align=""><input type="reset" value="Reset Form"></td>
                </tr>
                <tr>
                    <td colspan="2" align="center"><div id="disp" class="text"></div></td>
                </tr>
            </table>
        </form>
    Edit:
    FOR SOME REASON i CAN'T CONNECT TO UPLOAD RIGHT NOW.:dunno:
    Edit:
    COULD SOMEONE HELP ME SORT THIS OUT OVER MSN? Add me: airbrushkits@live.com
    :happysad:
    Last edited by goldy30; 12-02-2008 at 03:14 PM. Reason: Automerged Doublepost

  4. #4
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Javascript Help

    This link shows radio buttons in action:
    http://www.w3schools.com/js/tryit.as...yjs_form_radio

    Let me know if that link doesn't work for you or if you need more info.
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

  5. #5
    goldy30's Avatar
    goldy30 is offline x10Hosting Member goldy30 is an unknown quantity at this point
    Join Date
    Jul 2008
    Posts
    60

    Cool Re: Javascript Help

    Quote Originally Posted by quantum1 View Post
    This link shows radio buttons in action:
    http://www.w3schools.com/js/tryit.as...yjs_form_radio

    Let me know if that link doesn't work for you or if you need more info.
    Thats not quite what I'm after... It need to check in order of each input in the form, then I need an alert to pop up if either one of the radio buttons isn't checked, otherwise return true and check the next input.

  6. #6
    quantum1's Avatar
    quantum1 is offline x10Hosting Member quantum1 is an unknown quantity at this point
    Join Date
    Sep 2008
    Location
    near Nashville, TN
    Posts
    68

    Re: Javascript Help

    Try changing your javascript checkGender to be as below:

    //GENDER VALIDATION //////////////////////////////////////////////////////////
    function checkGender(elem, helperMsg){
    //alert(elem.value)
    if(elem.value != "male" && elem.value != "female"){
    alert(helperMsg);
    elem.focus();
    return false;
    }else{
    return true;
    }
    Two rules of development:
    1) Computers work for people; People do not work for computers
    2) Maintainability is all that matters.

+ Reply to Thread

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  4. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM
  5. Some nice-simple JavaScript effects !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-13-2006, 08:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers