Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: validating form

  1. #1
    New Coder
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    validating form

    I managed to create the form that asks you to type in the information but I'm having some difficulty trying to figure out the alert to say "Thank you " after you have everything filled in and then hitting the button.

    my code is

    Code:
    <HTML>
        <HEAD>
        <TITLE> Form Validation Example </TITLE>
        <SCRIPT LANGUAGE="JavaScript">
    
        function validatePersonalInfo(){
          var _first = document.info.fname.value;
          var _city = document.info.city.value;
          var _phone = document.info.phone.value;
    
          if(_first.toString() == ""){alert("Please enter a first name.");}
          if(_city.toString() == ""){alert("Please enter your city.");}
          if(_phone.toString() == ""){alert("Please enter your phone number.");}
    
            var phoneInput = document.info.phone.value;
            var validPhone = false;
            var validCity = false;
             if(checkCity == true){
                validCity = true;
             }
             else{
    
             if(!checkPhone(phoneInput)){
                   alert("Phone number is invalid." + validPhone);
                }
             else{
                 validPhone = true;
             }
             if(validCity && validPhone){
                alert("Your form has been verified");
             }
        }
    }
    
        function checkPhone(str){
          var regexp = /^(\d{10}|\d{3}-\d{3}-\d{4}|\(\d{3}\)\d{3}-\d{4})$/;
           return regexp.test(str);
        }
        function checkNum(length){
          var cityLet = parseInt(cityEntry, 10);
          if (document.info.city.value.length == length){
            if(cityLet != 0 && isNaN(cityLet) == false){
              return true;
            }
            else {
              return false;
            }
          }
          else {
            return false;
          }
        }
    
    </script>
    </head>
    
        <body>
        <p>
        <form name="info"  action="" method="post">
        <table>
        <tr><td align="left">First Name:</td>
        <td align="left">
        <input type="text" name="fname" size=15>
        </td>
        </tr>
        <br>
        </tr>
        <br>
        <tr>
        <td align="left">City:</td>
        <td align="left">
        <input type="text" name="city" size=15>
        </td>
        </tr>
        <br>
        <tr><td align="left">phone</td>
        <td align="left">
        <input type="text" name="phone" size=20></td>
        </tr>
        <br>
        </tr>
        <br>
        </table>
        <center>
        <input type="button" value="Submit" onClick="validatePersonalInfo()">
        </center>
        </form>
        </body>
        </html>
    the help is greatly appreciated

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Standard reply #1: Use the error console.


  •  

    Posting Permissions

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