SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Hey can someone have a look at this for me

    Hey can someone have a look at this for me
    Hey can someone have a look at this java script for me, i have made it following this info from this site http://www.tizag.com/javascriptT/javascriptform.php but for some reason mine still wont work :( can someone have a look and tell me what i have done wrong? and how i can corect the script? this is a project for collage and i only have 16 days left to do it :(




    <html>
    <head>
    <title>Joe's Fruit Shop Order Form</title>

    <script type='text/javascript'>
    function formValidator(){
    var fullname = document.getElementById('fullname');
    var home = document.getElementById('home');
    var mobile = document.getElementById('mobile');
    var work = document.getElementById('work');
    var email = document.getElementById('email');
    var address = document.getElementById('address');
    var citysub = document.getElementById('citysub');
    var state = document.getElementById('state');
    var post = document.getElementById('post');
    var cardtype = document.getElementById('cardtype');
    var month = document.getElementById('month');
    var year = document.getElementById('year');
    var number = document.getElementById('number');
    var holder = document.getElementById('holder');
    var item1 = document.getElementById('item1');
    var quantity1 = document.getElementById('quantity1');
    var item2 = document.getElementById('item2');
    var quantity2 = document.getElementById('quantity2');
    var item3 = document.getElementById('item3');
    var quantity3 = document.getElementById('quantity3');
    var item4 = document.getElementById('item4');
    var quantity4 = document.getElementById('quantity4');
    var item5 = document.getElementById('item5');
    var quantity5 = document.getElementById('quantity5');


    if(isAlphabet(fullname, "Please enter only letters for your full name")){
    if(isNumeric(home, "Please enter a valid home number")){
    if(emailValidator(email, "Please enter a valid email address")){
    if(isAlphanumeric(address, "Please enter Numbers and Letters Only for Address")){
    if(isAlphabet(citysub, "Please enter only letters for your City/Suburb")){
    if(madeSelection(state, "Please Choose a State")){
    if(isNumeric(post, "Please enter a valid post code")){
    if(madeSelection(cardtype, "Please select a Credit Card Type")){
    if(madeSelection(month, "Please select your Credit Card Expiry Month")){
    if(madeSelection(year, "Please select your Credit Card Expiry Year")){
    if(isNumeric(number, "Please enter a valid Credit Card Number")){
    if(isAlphabet(holder, "Please enter only letters for the Card Holders name")){
    if(madeSelection(item1, "Please select an Item")){
    if(madeSelection(quantity1, "Please select the Quantity of the item you would like")){
    if(madeSelection(item2, "Please select an Item")){
    if(madeSelection(quantity2, "Please select the Quantity of the item you would like")){
    if(madeSelection(item3, "Please select an Item")){
    if(madeSelection(quantity3, "Please select the Quantity of the item you would like")){
    if(madeSelection(item4, "Please select an Item")){
    if(madeSelection(quantity4, "Please select the Quantity of the item you would like")){
    if(madeSelection(item5, "Please select an Item")){
    if(madeSelection(quantity5, "Please select the Quantity of the item you would like")){
    return true;
    }
    }
    }
    }
    }

    }
    return false;

    }

    function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
    return true;
    }else{
    alert(helperMsg);
    elem.focus();
    return false;
    }
    }

    function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
    return true;
    }else{
    alert(helperMsg);
    elem.focus();
    return false;
    }
    }

    function emailValidator(elem, helperMsg){
    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;
    }
    }

    function isAlphanumeric(elem, helperMsg){
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
    return true;
    }else{
    alert(helperMsg);
    elem.focus();
    return false;
    }
    }function madeSelection(elem, helperMsg){
    if(elem.value == "Please Choose"){
    alert(helperMsg);
    elem.focus();
    return false;
    }else{
    return true;
    }
    }




    </script>

    </head>
    <body>
    <h2>Joe's Fruit Shop Order Form</h2> <p>Your details and delivery address: Regions that contain a "*" are required</p>

    <form onsubmit='return formValidator()' >

    <table border="1">

    <tr>
    <td>*Full Name:</td>
    <td><input type='text' id='fullname'/></td>
    <td>*Home Phone:</td>
    <td><input type="text" id="home"/></td>
    </tr>

    <tr>
    <td>Mobile Phone:</td>
    <td><input type="text" id="mobile"/></td>
    <td>Work Phone:</td>
    <td><input type="text" id="work"/></td>
    </tr>

    <tr>
    <td>*E-Mail:</td>
    <td><input type="text" id="email"/></td>
    <td>*Address:</td>
    <td><input type="text" id="address"/></td>
    </tr>

    <tr>
    <td>*City Suburb:</td>
    <td><input type="text" id="citysub"/></td>
    <td>*State:</td>
    <td>
    <SELECT id="state">
    <OPTION SELECTED value="">State
    <OPTION value="1">QLD</option>
    <OPTION value="2">NSW</option>
    <OPTION value="3">ACT</option>
    <OPTION value="4">VIC</option>
    <OPTION value="5">TAS</option>
    <OPTION value="6">SA</option>
    <OPTION value="7">NT</option>
    <OPTION value="8">WA</option>
    </SELECT>
    </td>
    </tr>
    <tr>
    <td>*Post Code:</td>
    <td><input type="text" id="post"/></td>
    </tr>
    </table>

    <hr>
    <p>*Credit card details:</p>

    <table border="1">
    <tr>
    <td>*Card Type</td>
    <td>
    <select id="cardtype">
    <option>Please select card type</option>
    <option value="1">Visa</option>
    <option value="2">Master Card</option>
    <option value="3">AMX</option>
    </select>
    </td>
    <td>*Expiry date:</td>
    <td>
    <select id="month">
    <OPTION SELECTED value="">Month
    <option value="1">Jan</option>
    <option value="2">Fab</option>
    <option value="3">Mar</option>
    <option value="4">Apr</option>
    <option value="5">May</option>
    <option value="6">Jun</option>
    <option value="7">Jul</option>
    <option value="8">Aug</option>
    <option value="9">Sep</option>
    <option value="10">Oct</option>
    <option value="11">Nov</option>
    <option value="12">Dec</option>
    </select>
    <select id="year">
    <OPTION SELECTED value="">Year</option>
    <option value="1">2009</option>
    <option value="2">2010</option>
    <option value="3">2011</option>
    <option value="4">2012</option>
    <option value="5">2013</option>
    </select>
    </td>
    </tr>

    <tr>
    <td>*Card Number:</td>
    <td><input type="text" id="number"/></td>
    <td>*Card holder's name:</td>
    <td><input type="text" id="holder"/></td>
    </tr>
    </table>

    <hr>
    <p>*Purchase details:</p>

    <table border="1">
    <tr><td>*Item choises and Quantity.</td></tr>
    <tr><td>
    <select id="item1">
    <OPTION SELECTED value="">Item</option>
    <option value="1">Alfalfa sprouts $1.50 per punnet</option>
    <option value="2">Apples, Bonza $5.99KG</option>
    <option value="3">Apples, Golden Delicious $4.99KG</option>
    <option value="4">Artichoke hearts, pickled $17.99KG</option>
    <option value="5">Avocados, dark, large 2 for $3.50</option>
    </select>
    <select id="quantity1">
    <OPTION SELECTED value="">Quantity</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    </tr></td>

    <tr><td>
    <select id="item2">
    <OPTION SELECTED value="0">Item</option>
    <option value="1">Alfalfa sprouts $1.50 per punnet</option>
    <option value="2">Apples, Bonza $5.99KG</option>
    <option value="3">Apples, Golden Delicious $4.99KG</option>
    <option value="4">Artichoke hearts, pickled $17.99KG</option>
    <option value="5">Avocados, dark, large 2 for $3.50</option>
    </select>
    <select id="quantity2">
    <OPTION SELECTED value="0">Quantity</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    </tr></td>

    <tr><td>
    <select id="item3">
    <OPTION SELECTED value="0">Item</option>
    <option value="1">Alfalfa sprouts $1.50 per punnet</option>
    <option value="2">Apples, Bonza $5.99KG</option>
    <option value="3">Apples, Golden Delicious $4.99KG</option>
    <option value="4">Artichoke hearts, pickled $17.99KG</option>
    <option value="5">Avocados, dark, large 2 for $3.50</option>
    </select>
    <select id="quantity3">
    <OPTION SELECTED value="0">Quantity</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    </tr></td>

    <tr><td>
    <select id="item4">
    <OPTION SELECTED value="0">Item</option>
    <option value="1">Alfalfa sprouts $1.50 per punnet</option>
    <option value="2">Apples, Bonza $5.99KG</option>
    <option value="3">Apples, Golden Delicious $4.99KG</option>
    <option value="4">Artichoke hearts, pickled $17.99KG</option>
    <option value="5">Avocados, dark, large 2 for $3.50</option>
    </select>
    <select id="quantity4">
    <OPTION SELECTED value="0">Quantity</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    </tr></td>

    <tr><td>
    <select id="item5">
    <OPTION SELECTED value="0">Item</option>
    <option value="1">Alfalfa sprouts $1.50 per punnet</option>
    <option value="2">Apples, Bonza $5.99KG</option>
    <option value="3">Apples, Golden Delicious $4.99KG</option>
    <option value="4">Artichoke hearts, pickled $17.99KG</option>
    <option value="5">Avocados, dark, large 2 for $3.50</option>
    </select>
    <select id="quantity5">
    <OPTION SELECTED value="0">Quantity</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10">10</option>
    </select>
    </tr></td>
    </table>

    <table>
    <tr><td>Click <b>Submit</b> to submit the order form, click <b>Reset</b> to reset the form.</td></tr>
    <tr>
    <td>


    <input type='submit' value='Submit'/>




    </td>
    <td><input type="reset" name="reset"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't look at it deeply.
    Anyway, I think you always miss .val() when getting the value.
    Code:
    var fullname = document.getElementById('fullname');
    should be
    Code:
    var fullname = document.getElementById('fullname').val();
    ArticleQueen: First Article Rewriter with Lexical Processor!

  3. #3
    SitePoint Member
    Join Date
    May 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to add up, your open and close curly braces {} seem to be not matched. Better make sure that everytime you write an open curly brace {, you will as well write close brace } to prevent errors.

    Also, this block of codes:
    Code JavaScript:
    if(isAlphabet(fullname, "Please enter only letters for your full name")){}
    if(isNumeric(home, "Please enter a valid home number")){}
    if(isAlphabet(fullname, "Please enter only letters for your full name")){
    if(isNumeric(home, "Please enter a valid home number")){
    if(emailValidator(email, "Please enter a valid email address")){
    if(isAlphanumeric(address, "Please enter Numbers and Letters Only for Address")){
    if(isAlphabet(citysub, "Please enter only letters for your City/Suburb")){
    if(madeSelection(state, "Please Choose a State")){
    if(isNumeric(post, "Please enter a valid post code")){
    if(madeSelection(cardtype, "Please select a Credit Card Type")){
    if(madeSelection(month, "Please select your Credit Card Expiry Month")){
    if(madeSelection(year, "Please select your Credit Card Expiry Year")){
    if(isNumeric(number, "Please enter a valid Credit Card Number")){
    if(isAlphabet(holder, "Please enter only letters for the Card Holders name")){
    if(madeSelection(item1, "Please select an Item")){
    if(madeSelection(quantity1, "Please select the Quantity of the item you would like")){
    if(madeSelection(item2, "Please select an Item")){
    if(madeSelection(quantity2, "Please select the Quantity of the item you would like")){
    if(madeSelection(item3, "Please select an Item")){
    if(madeSelection(quantity3, "Please select the Quantity of the item you would like")){
    if(madeSelection(item4, "Please select an Item")){
    if(madeSelection(quantity4, "Please select the Quantity of the item you would like")){
    if(madeSelection(item5, "Please select an Item")){
    if(madeSelection(quantity5, "Please select the Quantity of the item you would like")){
    return true;
    }
    }
    }
    }
    }
     
    }
    return false;
     
    }

    Instead of having a nested if's on the above code block, you should try to have a separate/individual if's. Why? So that it will still enter other conditional statements even when the fullname has error, also it will be lot easier for you to track down what's wrong with your codes and plus of course it will be readable.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by vnviews View Post
    I didn't look at it deeply.
    Anyway, I think you always miss .val() when getting the value.
    Code:
    var fullname = document.getElementById('fullname');
    should be
    Code:
    var fullname = document.getElementById('fullname').val();
    1) The values are taken in the actual validation functions. So no need to change this
    2) .val() is jQuery. In pure javascript it's just .value
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    May 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simply_jhen View Post
    Just to add up, your open and close curly braces {} seem to be not matched. Better make sure that everytime you write an open curly brace {, you will as well write close brace } to prevent errors.

    Also, this block of codes:
    Code JavaScript:
    if(isAlphabet(fullname, "Please enter only letters for your full name")){}
    if(isNumeric(home, "Please enter a valid home number")){}
    if(isAlphabet(fullname, "Please enter only letters for your full name")){
    if(isNumeric(home, "Please enter a valid home number")){
    if(emailValidator(email, "Please enter a valid email address")){
    if(isAlphanumeric(address, "Please enter Numbers and Letters Only for Address")){
    if(isAlphabet(citysub, "Please enter only letters for your City/Suburb")){
    if(madeSelection(state, "Please Choose a State")){
    if(isNumeric(post, "Please enter a valid post code")){
    if(madeSelection(cardtype, "Please select a Credit Card Type")){
    if(madeSelection(month, "Please select your Credit Card Expiry Month")){
    if(madeSelection(year, "Please select your Credit Card Expiry Year")){
    if(isNumeric(number, "Please enter a valid Credit Card Number")){
    if(isAlphabet(holder, "Please enter only letters for the Card Holders name")){
    if(madeSelection(item1, "Please select an Item")){
    if(madeSelection(quantity1, "Please select the Quantity of the item you would like")){
    if(madeSelection(item2, "Please select an Item")){
    if(madeSelection(quantity2, "Please select the Quantity of the item you would like")){
    if(madeSelection(item3, "Please select an Item")){
    if(madeSelection(quantity3, "Please select the Quantity of the item you would like")){
    if(madeSelection(item4, "Please select an Item")){
    if(madeSelection(quantity4, "Please select the Quantity of the item you would like")){
    if(madeSelection(item5, "Please select an Item")){
    if(madeSelection(quantity5, "Please select the Quantity of the item you would like")){
    return true;
    }
    }
    }
    }
    }
     
    }
    return false;
     
    }

    Instead of having a nested if's on the above code block, you should try to have a separate/individual if's. Why? So that it will still enter other conditional statements even when the fullname has error, also it will be lot easier for you to track down what's wrong with your codes and plus of course it will be readable.
    could you give me an example of what you mean by individual if's in the code ? lol soz im very very very new to java

  6. #6
    SitePoint Member
    Join Date
    May 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey all fixed now thanks for the help guys it works perfect now lolz


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
  •