Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
validating a contact form
Old 09-16-2010, 08:13 AM validating a contact form
NickH's Avatar
Extreme Talker

Posts: 155
Trades: 0
hey all,

looking for some help with javascript and validating a form.
so what im trying to do is ive got a form and when the user clicks submit if fields aren't filled in an alert will pop up that asks the user to fill in the remaining fields.

im having a few problems with this, mainly that the alert wont pop up so if any one is willing to have a look at the code and then let me know where im going wrong that would be excellent.

HTML
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <!--- Created by Nick Hayden (29/07/2010) -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Little Bean Order Form</title>
        <script type="text/javascript" src="../javascript/script.js" ></script>
    <style type="text/css">
        body 
        {
            background-color:#FF6600;
            font-family:Verdana, Arial, Helvetica, sans-serif;
            font-size:12pt;
        }
        
    </style>
    </head>
    <body>
        
    <table border="0" width="960" align="center">

    <tr>

    <td colspan="2"> <a href="../homepage.html"> <img src="../images/coffeebeans.jpg" alt="Coffee Shop Header" 
                     height="100" width="960" /></a> </td>

    </tr>
    
    <tr>

    <td valign="top" align="center" style="width:150px;">
                <p>
                    <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />    
                        <br />
                        <a href="../homepage.html">Home</a>
                        <br /><br />
                        <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />
                        <br />
                        <a href="../html/coffee.html">Coffee</a>                        
                        <br /><br />
                        <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />
                        <br />
                        <a href="../html/menu.html">Menu</a>
                        <br /><br />
                </p></td>

    <td valign="top">
    <h1 align="center" style="color:#CC3300">Order Form</h1>
    
    <hr /> 
        
    <form align="justify" name="order">    
        <p>
        First Name:
        <input type="text" name="fname" onfocus="fname()"/>
        Last Name:
        <input type="text" name="lname" value=""/><br /><br />
        Address:
        <input type="text" name="address" value="" />
        Suburb:
        <input type="text" name="suburb" value="" /><br /><br />
        State
        <select name="state">
            <option>Please select your State</option>
            <option>Australian Capital Territory (ACT)</option>
            <option>New South Wales (NSW)</option>
            <option>Northern Territory (NT)</option>
            <option>Queensland (QLD)</option>
            <option>South Australia (SA)</option>
            <option>Tasmania (TAS)</option>
            <option>Victoria (VIC)</option>
            <option>Western Australia (WA)</option>
        </select>
        Postcode: 
        <input type="text" name="postcode" value="" /><br /><br />
        Email:
        <input type="text" name="email" value="" />
        Phone Number:
        <input type="text" name="phone" value="" /><br /><br />
        <hr />
        Order:<br />
        <textarea rows="10" cols="70" name="order" value="" ></textarea>
        <br /><br />
        <hr />
        Select Payment:<br />
        <input type="radio" name="payment" value="credit" /> Credit Card<br />
        <input type="radio" name="payment" value="paypal" /> Paypal <br />
        <input type="radio" name="payment" value="bank" /> Online Baking <br />
        <hr />
        
        <input type="submit" value="Submit Order" name="submit" onclick="fname()" />
        <input type="reset" value="Reset Form" name="cancel" />
        
    </form>
     
    <div style="text-align:center; font-size:10pt;">
    <p><a href="../html/contact.html">Contact Us</a> | <a href="../html/copyright.html">Copyright</a> | <a href="../html/privacy.html">Privacy</a> | <a href="mailto:nickhayden1992@gmail.com">Web Administrator</a></p>
        
    </div>
    </td>

    </tr>
    
    </table>

        
    </body>
</html>
Javascript

Code:
    function fname()
        {
             (document.form.order.input.fname=="");
                {
                    alert("Please Enter Your Name");
                }
        }

thanks for any reply
__________________

Please login or register to view this content. Registration is FREE
NickH is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-17-2010, 08:47 AM Re: validating a contact form
NickH's Avatar
Extreme Talker

Posts: 155
Trades: 0
alright so i've fixed my code that now lets me validate the fields to check if they have some text inside them now i have another problem,

how do i allow only numbers into a text field?

Code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <!--- Created by Nick Hayden (29/07/2010) -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Little Bean Order Form</title>
        <script language="javascript" src="../javascript/script.js" ></script>
    <style type="text/css">
        body 
        {
            background-color:#FF6600;
            font-family:Verdana, Arial, Helvetica, sans-serif;
            font-size:12pt;
        }
        
    </style>
    </head>
    <body>
        
    <table border="0" width="960" align="center">

    <tr>

    <td colspan="2"> <a href="../homepage.html"> <img src="../images/coffeebeans.jpg" alt="Coffee Shop Header" 
                     height="100" width="960" /></a> </td>

    </tr>
    
    <tr>

    <td valign="top" align="center" style="width:150px;">
                <p>
                    <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />    
                        <br />
                        <a href="../homepage.html">Home</a>
                        <br /><br />
                        <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />
                        <br />
                        <a href="../html/coffee.html">Coffee</a>                        
                        <br /><br />
                        <img src="../images/coffee-bean.png" alt="Coffee Bean Navigation Image" width="50" />
                        <br />
                        <a href="../html/menu.html">Menu</a>
                        <br /><br />
                </p></td>

    <td valign="top">
    <h1 align="center" style="color:#CC3300">Order Form</h1>
    
    <hr /> 
        
    <form align="justify" name="orderform" id="order" onSubmit="return checkform(this)">    
        <p>
        First Name:
        <input type="text" name="fname" />
        Last Name:
        <input type="text" name="lname" value=""/><br /><br />
        Address:
        <input type="text" name="address" value="" />
        Suburb:
        <input type="text" name="suburb" value="" /><br /><br />
        State
        <select name="state">
            <option>Please select your State</option>
            <option>Australian Capital Territory (ACT)</option>
            <option>New South Wales (NSW)</option>
            <option>Northern Territory (NT)</option>
            <option>Queensland (QLD)</option>
            <option>South Australia (SA)</option>
            <option>Tasmania (TAS)</option>
            <option>Victoria (VIC)</option>
            <option>Western Australia (WA)</option>
        </select>
        Postcode: 
        <input type="text" name="postcode" value="" /><br /><br />
        Email:
        <input type="text" name="email" value="" />
        Phone Number:
        <input type="text" name="phone" value="" /><br /><br />
        <hr />
        Order:<br />
        <textarea rows="10" cols="70" name="order" value="" ></textarea>
        <br /><br />
        <hr />
        Select Payment:<br />
        <input type="radio" name="payment" value="credit" /> Credit Card<br />
        <input type="radio" name="payment" value="paypal" /> Paypal <br />
        <input type="radio" name="payment" value="bank" /> Online Baking <br />
        <hr />
        
        <input type="submit" value="Submit Order" name="submit" />
        <input type="reset" value="Reset Form" name="cancel" />
        
    </form>
     
    <div style="text-align:center; font-size:10pt;">
    <p><a href="../html/contact.html">Contact Us</a> | <a href="../html/copyright.html">Copyright</a> | <a href="../html/privacy.html">Privacy</a> | <a href="mailto:nickhayden1992@gmail.com">Web Administrator</a></p>
        
    </div>
    </td>

    </tr>
    
    </table>

        
    </body>
</html>
Code:
 

    function checkform ( form )
        {
            if (form.fname.value == "") 
                {
                    alert ( "Please Enter Your First Name." );
                    form.fname.focus();
                    return false;
                }
            
            if (form.lname.value == "")
                {
                    alert ( "Please enter your Last Name." ); 
                    form.lname.focus(); 
                    return false; 
                }
                
            if (form.address.value == "") 
                {
                    alert ( "Please enter your Address." ); 
                    form.address.focus();
                    return false; 
                }
                
            if (form.suburb.value == "")
                {
                    alert ( "Please enter your Suburb." ); 
                    form.suburb.focus(); 
                    return false; 
                }
                
            if (form.state.value == "Please select your State" ) 
                {
                    alert ( "Please choose your State" ); 
                    form.state.focus(); 
                    return false; 
                }
            
            if (form.postcode.value == "" )
                {
                    alert ("Please enter your Postcode" );
                    form.postcode.focus();
                    return false; 
                }
            
            if (form.email.value == "")
                {
                    alert ("Please enter your Email" ); 
                    form.email.focus(); 
                    return false; 
                }
                
            if (form.phone.value == "")
                {
                    alert ("Please enter your Phonenumber" ); 
                    form.phone.focus(); 
                    return false; 
                }
            
            if (form.order.value == "") 
                {
                    alert ( "Please enter your Order" );
                    form.order.focus(); 
                    return false; 
                }
        
        return true;        
        }
__________________

Please login or register to view this content. Registration is FREE
NickH is offline
Reply With Quote
View Public Profile
 
Old 09-17-2010, 09:04 AM Re: validating a contact form
Ultra Talker

Posts: 366
Name: Steve
Location: Miami, FL, Earth
Trades: 0
if (isNaN(value)) {
return false;
}
__________________
- Steve

President,
Please login or register to view this content. Registration is FREE
smoseley is offline
Reply With Quote
View Public Profile Visit smoseley's homepage!
 
Old 09-17-2010, 09:59 AM Re: validating a contact form
NickH's Avatar
Extreme Talker

Posts: 155
Trades: 0
hey mate, thanks for the reply but im still having a bit of trouble with it at the moment the code looks like

Code:
if (form.postcode.value == "" )
                {
                    alert ("Please enter your Postcode" );
                    form.postcode.focus();
                    return false; 
                }
                
                if (form.postcode.value == isNaN(value)) 
                    {
                    alert ("Please enter only numbers"); 
                    form.postcode.focus();
                    return false;
                    }
and yeah im just struggling with how to make it only numbers going into the field
__________________

Please login or register to view this content. Registration is FREE
NickH is offline
Reply With Quote
View Public Profile
 
Old 09-17-2010, 10:34 AM Re: validating a contact form
NickH's Avatar
Extreme Talker

Posts: 155
Trades: 0
so after some further fiddling around i've got it so that if there are letters in the field then an alert pops up but im still looking to get only numbers entered into the field

Code:
 if (form.postcode.value = isNaN()) 
                    {
                    alert ("Please enter only numbers"); 
                    form.postcode.focus();
                    return false;
                    }
__________________

Please login or register to view this content. Registration is FREE
NickH is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to validating a contact form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16331 seconds with 12 queries