can somebody please explain this javascript to me ?

Discussion in 'JavaScript' started by gether, Jul 18, 2013.

  1. #1
    [​IMG]


    [​IMG]
     
    gether, Jul 18, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    The code is wrong and won't work but it's for checking form field values and testing them with a regular expression to check if they are lower case or upper case letters a to z. If it fails it will alert a the message passed to the function.

    I would of wrote the code correctly for you but I can't select the text.
     
    HuggyStudios, Jul 18, 2013 IP
  3. gether

    gether Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    so this is like ...

    if isalphabet isallthat
    true
    else not true

    ?
     
    gether, Jul 18, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    Yes that's how it should be but that code isn't right. If you post the actual code I can fix for you.
     
    HuggyStudios, Jul 18, 2013 IP
  5. gether

    gether Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    the full code is a lot of form feild validation ..a feild validation for every feild ... wait i will post .. but before that ....

    this is like checking a variable x against another variable y ?

    is this how you declare regular expression then ?

    var x = /^ +$/ ? not the entire possible characters in the keyboard ?
     
    gether, Jul 18, 2013 IP
  6. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #6
    Yes that is how you set it but it should have (a-zA-Z) after the ^
     
    HuggyStudios, Jul 18, 2013 IP
  7. gether

    gether Member

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #7
    like /^a-zA-Z+$/ ? with that extra +$

    ?

    <script type='text/javascript'>
     
    function formValidator(){
        // Make quick references to our fields
        var firstname = document.getElementById('firstname');
        var addr = document.getElementById('addr');
        var zip = document.getElementById('zip');
        var state = document.getElementById('state');
        var username = document.getElementById('username');
        var email = document.getElementById('email');
     
        // Check each input in the order that it appears in the form!
        if(isAlphabet(firstname, "Please enter only letters for your name")){
            if(isAlphanumeric(addr, "Numbers and Letters Only for Address")){
                if(isNumeric(zip, "Please enter a valid zip code")){
                    if(madeSelection(state, "Please Choose a State")){
                        if(lengthRestriction(username, 6, 8)){
                            if(emailValidator(email, "Please enter a valid email address")){
                                return true;
                            }
                        }
                    }
                }
            }
        }
     
     
        return false;
     
    }
     
    function notEmpty(elem, helperMsg){
        if(elem.value.length == 0){
            alert(helperMsg);
            elem.focus(); // set the focus to this input
            return false;
        }
        return true;
    }
     
    function isNumeric(elem, helperMsg){
        var numericExpression = /^[0-9]+$/;
        if(elem.value.match(numericExpression)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            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 isAlphanumeric(elem, helperMsg){
        var alphaExp = /^[0-9a-zA-Z]+$/;
        if(elem.value.match(alphaExp)){
            return true;
        }else{
            alert(helperMsg);
            elem.focus();
            return false;
        }
    }
     
    function lengthRestriction(elem, min, max){
        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;
        }
    }
     
    function madeSelection(elem, helperMsg){
        if(elem.value == "Please Choose"){
            alert(helperMsg);
            elem.focus();
            return false;
        }else{
            return true;
        }
    }
     
    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;
        }
    }
    </script>
    
    Code (markup):

    
    <html>
    <head>
    <script>
     
    <form onsubmit='return formValidator()' >
    First Name: <input type='text' id='firstname' /><br />
    Address: <input type='text' id='addr' /><br />
    Zip Code: <input type='text' id='zip' /><br />
    State: <select id='state'>
        <option>Please Choose</option>
        <option>AL</option>
        <option>CA</option>
        <option>TX</option>
        <option>WI</option>
    </select><br />
    Username(6-8 characters): <input type='text' id='username' /><br />
    Email: <input type='text' id='email' /><br />
    <input type='submit' value='Check Form' /><br />
    </form>
     
    </script>
    </head>
    </html>
    
    Code (markup):
     
    Last edited: Jul 18, 2013
    gether, Jul 18, 2013 IP
  8. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #8
    HuggyStudios, Jul 18, 2013 IP