Closed Thread
Results 1 to 3 of 3

Thread: I can't fine the error in this.

  1. #1
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    I can't fine the error in this.

    Hi there, I'm sure it's something simple, but I can't find it!

    Basically, little validation code for my form. I'm attempting to check it nothing has been entered in a text box.

    The check-box part works, but I can't get the other validation statements to work!

    Code:
    function validate(form) {
    	if(!document.orderForm.accept.checked){
    		alert("You must accept the terms and conditions of sale before proceeding."); 
    		return false;
    	}
    	else if(document.getElementById('firstName').value == NULL){
    		alert ("You need to enter your first name.");
    		return false;	
    	} 
    	else if(document.getElementById('lastName').value == NULL) {
    		alert ("You need to enter your last name.");
    		return false;
    	}
    	else {
    		return true;
    	}
    }
    HTML Code:
    <form action="orderProc.php" name="orderForm" method="post" onSubmit='return validate(this)'>
    				<p class="contentText"><b>Contact Details<br /><br /></b>First Name: </p>
    				<input type="text" id="firstName" name="firstName" size="20" maxlength="40" /><br /><br />
    				<p class="contentText">Last Name: </p>
    				<input type="text" name="lastName" size="20" maxlength="40" /><br /><br />
    <p class="contentText"> <input type="checkbox" name="accept" value="no"> I accept the <a class="ip" href="http://sflmaterials.co.uk/TC.pdf" style="text-decoration:underline;" target="_blank">terms and conditions of sale.</a></p><br /><br />
    				<input type="submit" name="placeOrder" value="Place Order" />
    				</form>
    Thanks in advance!
    Last edited by so1i; 11-23-2009 at 04:34 AM. Reason: EDIT: Sorry about typo in title!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: I can't fine the error in this.

    A few points

    1. You've not given your lastName input an id

    2. Javascript is case sensitive. You should be using null rather than NULL

    3. If no text is entered in the text input then it's value is an empty string (in firefox at least) and an empty string isn't null. If you are only checking to make sure the text input isn't empty then you are better off using
    [highlight="javascript"]
    if (document.getElelementById('firstname').value == "")
    [/highlight]
    If there's a new way, I'll be the first in line.

    But, it better work this time.

  4. #3
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: I can't fine the error in this.

    Quote Originally Posted by Hignar View Post
    A few points

    1. You've not given your lastName input an id

    2. Javascript is case sensitive. You should be using null rather than NULL

    3. If no text is entered in the text input then it's value is an empty string (in firefox at least) and an empty string isn't null. If you are only checking to make sure the text input isn't empty then you are better off using
    [highlight="javascript"]
    if (document.getElelementById('firstname').value == "")
    [/highlight]
    Cheers mate, appreciated.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 06-20-2010, 10:52 PM
  2. Java Help - Compiles Fine, But Error on Running?
    By UnknownFear in forum Java Help
    Replies: 8
    Last Post: 06-14-2010, 05:11 AM
  3. My error-free code has an error. (Switches/Cases)
    By Sundosia in forum C and C++
    Replies: 3
    Last Post: 08-07-2008, 12:16 PM
  4. Replies: 1
    Last Post: 05-06-2008, 05:16 AM

Tags for this Thread

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