Validation Form

Discussion in 'HTML & Website Design' started by Ellys Kho, Mar 7, 2013.

  1. #1
    here the javascript
    <SCRIPT LANGUAGE="JavaScript">
     
    function validateform(theform) {
     
    if (theform.name.value == "") {
    alert("Please enter your name");
    theform.name.focus();
    return false;
    }
     
    if (theform.url.value == "http://") {
    alert("Please enter your website URL");
    theform.url.focus();
    return false;
    }
     
    if (theform.email.value == "") {
    alert("Please enter your E-Mail Address");
    theform.email.focus();
    return false;
    }
     
    if (theform.title.value == "") {
    alert("Please enter your Website Title");
    theform.title.focus();
    return false;
    }
     
    if (!theform.tick.checked) {
    alert("You must agree with the Terms & Conditions");
    theform.tick.focus();
    return false;
    }
    }
     
    </script>
    Code (markup):
    then here the html form

    <form method="POST" onsubmit="return validateform(this)" action="testing.php">
                                <table height="249" cellspacing="1" style="padding-left: 4px; padding-right: 4px; padding-top: 1px; padding-bottom: 1px" id="table3">
                                <tr>
                                    <td width="535" height="10" valign="bottom" colspan="2" style="font-family: Arial">
                                    </td>
                                </tr>
                                <tr>
                                    <td width="147" height="24" style="font-family: Arial" align="right">
                                    <b>
                                    <font size="2" style="font-family: Arial">Name:</font></b></td>
                                    <td width="385" height="24" style="font-family: Arial">
                                    <input type="text" name="name" size="55" style="width: 332; background-color:#FFFAE6; height:22" onblur="this.className=''" onfocus="this.className='ab'"></td>
                                </tr>
                                <tr>
                                    <td width="147" height="3" style="font-family: Arial" align="right">
                                    <b><font size="2" style="font-family: Arial">
                                    Website URL:</font></b></td>
                                    <td width="385" height="3" style="font-family: Arial">
                                    <input type="text" name="url" size="55" value="http://" style="width: 345; background-color:#FFFAE6; height:22" onblur="this.className=''" onfocus="this.className='ab'"></td>
                                </tr>
                                <tr>
                                    <td width="147" height="24" style="font-family: Arial" align="right">
                                    <b><font size="2" style="font-family: Arial">
                                    Email Address:</font></b></td>
                                    <td width="385" height="24" style="font-family: Arial">
                                    <input type="text" name="email" size="55"  style="width: 307; background-color:#FFFAE6; height:22" onblur="this.className=''" onfocus="this.className='ab'"></td>
                                </tr>
                                <tr>
                                    <td width="147" height="24" style="font-family: Arial" align="right">
                                    <b>
                                    <font size="2" style="font-family: Arial">Title
                                    Of Link:</font></b></td>
                                    <td width="385" height="24" style="font-family: Arial" valign="top">
                                    <input type="text" name="title" size="55"  style="width: 348; background-color:#FFFAE6; height:22" onblur="this.className=''" onfocus="this.className='ab'"></td>
                                </tr>
                                <tr>
                                    <td width="147" style="font-family: Arial">
                                    &nbsp;</td>
                                    <td width="385" style="font-family: Arial" valign="bottom">
                                    <b><font size="1">Conditions of Purchase</font></b></td>
                                </tr>
                                <tr>
                                    <td width="147" height="85" style="font-family: Arial">
                                    &nbsp;</td>
                                    <td width="385" height="85" style="font-family: Arial" valign="top">
                                        <textarea rows="9" name="S1" cols="47" style="background-color: #FFFAE6; font-size:8pt">
    Term of Services
    </textarea></td>
                                </tr>
                                <tr>
                                    <td width="147" height="22" style="font-family: Arial">&nbsp;</td>
                                    <td width="385" height="22" style="font-family: Arial">
                                    <p align="left">
                                    <font color="#D30D0D">
                                    <input id="agreebox" type="checkbox" value="1" name="tick" onblur="this.className=''" onfocus="this.className='ab'">
                                    <font size="1" style="font-family: Arial">I
                                    agree with the Conditions of Purchase set above.</font></font></td>
                                </tr>
                                <tr>
                                    <td width="147" height="54" style="font-family: Arial"></td>
                                    <td width="385" height="54" style="font-family: Arial">
                                    <input type="submit" value="Submit Your Data &gt;&gt;" name="B1" style="font-weight: bold; width:200" onblur="this.className=''" onfocus="this.className='ab'"></td>
                                </tr>
                            </table>
                        </form>
    
    Code (markup):
    i already try several times but always error. can you help me guys?
     
    Ellys Kho, Mar 7, 2013 IP
  2. khanals

    khanals Member

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    38
    #2
    i did not checked your code but when I reviewed you did wrong code on url validation. you have to use regular expression for URL validation
     
    khanals, Mar 7, 2013 IP
  3. Ellys Kho

    Ellys Kho Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #3
    if i test html code it's nothing wrong. but when i include the script it's error.

    sorry but i still newbie. maybe you need explain detail for expression.
     
    Ellys Kho, Mar 7, 2013 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Exactly what error and on exactly which line? "An error" doesn't give us much to go on.

    Also lose the table. There's no tabular data being displayed.
     
    Rukbat, Mar 8, 2013 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    What Rukbat is being polite about, I'm going to come right out and say -- the markup is a DISASTER. Tables for layout, tables around non-tabular data, paragraphs around non-paragraph data, tags and attributes like FONT and SIZE that have no business in ANY HTML written after 1998, inlined presentation, incomplete font-families, javascript 'onblur' and 'onfocus' doing CSS' job... It's a laundry list of how not to build a form.

    Like the others I'm stuck with more questions than answers -- like do you have this form working WITHOUT javascript first? You do know you have to validate it on the server too and that really should be made to work BEFORE you start adding 'scripting for nothing' -- which given the size of this form it could very well be scripting... for NOTHING.

    The script itself is horiffically outdated as well, with the long defunct LANGUAGE attribute, accessing form elements by name instead of getElementByID or walking the DOM, incomplete return status', no multi-checking so it's stuck doing one element at a time...

    I'd throw it all out and start over with MODERN coding techniques.

    Just to illustrate what I mean, there is likely zero legitimate reason for the markup of that form to be much more than:

    <form method="POST" action="testing.php" id="testing">
    	<fieldset>
    
    		<label for="testing_name">Name:</label>
    		<input type="text" name="name" id="testing_name" />
    		<br />
    
    		<label for="testing_url">Website URL:</label>
    		<input type="text" name="url" id="testing_url" value="http://" />
    		<br />
    
    		<label for="testing_email">Email Address:</label>
    		<input type="text" name="email" id="testing_email" />
    		<br />
    
    		<label for="testing_title">Title of Link:</label>
    		<input type="text" name="title" id="testing_title" />
    		<br />
    
    		<label for="testing_conditions">Conditions of Purchase</label>
    		<textarea name="S1" id="testing_conditions" rows="9" cols="47">Term of Services</textarea>
    		<br />
    
    		<label for="testing_agree" class="checkbox">
    			<input type="checkbox" name="tick" id="testing_agree" value="1" />
    			I agree with the Conditions of Purchase set above.
    		</label>
    
    		<input type="submit" class="submit" value="Submit Your Data &gt;&gt;" />
    
    	</fieldset>
    </form>
    Code (markup):
    EVERYTHING else you are doing there belongs in an external stylesheet said ONCE, not on every blasted line!
     
    Last edited: Mar 8, 2013
    deathshadow, Mar 8, 2013 IP
  6. Ellys Kho

    Ellys Kho Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #6
    i already try html form first & work. but when i want to add alert if table empy using Js. it's error on webpage.ahaha
     
    Ellys Kho, Mar 8, 2013 IP
  7. creativewebmaster

    creativewebmaster Active Member

    Messages:
    654
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    78
    #7
    Here is the great example to validate you from.
    function validateForm()
    {
    var x=document.forms["myForm"]["fname"].value;
    if (x==null || x=="")
      {
      alert("First name must be filled out");
      return false;
      }
    }
     
     
    <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    First name: <input type="text" name="fname">
    <input type="submit" value="Submit">
    </form>
    Code (markup):
     
    creativewebmaster, Mar 8, 2013 IP
  8. Ellys Kho

    Ellys Kho Guest

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #8
    oh. w3schools.com validation sample.
     
    Ellys Kho, Mar 9, 2013 IP