SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with validating a form.

    I'm having problems validating a form.

    This is my htm file.
    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" xml:lang="en" lang="en">
    <head>
    	<title>Labb5</title>
    	<link rel="stylesheet" type="text/css" href="css/style.css" />
    	<script type="text/javascript" src="js/script.js"></script>
    </head>
    
    
    <body>
    
    <form name="myForm" class="validate" onsubmit="return validate()" action="index.htm">
    	<p>
    	<label for="firstname">Förnamn</label>
    	<input type="text" name="firstnamn" />
    	<label for="lastname">Efternamn</label>
    	<input type="text" name="lastnamn" />
    	<label for="address">Adress</label>
    	<input type="text" name="address" />
    	<label for="city">Stad</label>
    	<input type="text" name="city" />
    	<label for="postalcode">Postadress</label>
    	<input type="text" name="postalcode" />
    	<label for="email">E-Mail adress</label>
    	<input type="text" name="email" />
    	<label for="phone">Telefon</label>
    	<input type="text" name="phone" />
    	<input type="submit" name="submit" value="Skicka" />
    	</p>
    </form>
    
    </body>
    </html>
    And here is my script.js file:

    Code:
    function validate() {
    	var x
    	x=document.person
    	
    	// Put form values into more easy to use variables.
    	aFirstname = x.firstname.value
    	aLastname = x.lastname.value
    	address = x.address.value
    	city = x.city.value
    	postalcode = x.postalcode.value
    	email = x.email.value
    	phone = x.phone.value
    	alert("skit")
    	
    	// Validate first and lastname.
    	if (aFirstname.length < 2) {
    		alert("Ditt förnamn har väl mer än 2 bokstäver?")
    	}
    	if (aLastname.length < 2) {
    		alert("Ditt efternamn har väl mer än 2 bokstäver?")
    	}
    }
    But when i submit the form nothing happends, I dont even get the skit alert that is supposed to be done everytime.

    Whats wrong?

    Thanks in advance!

    Martin Lundberg
    Sweden

  2. #2
    SitePoint Enthusiast BarbaraStreisand's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you certain that the path to the .JS file is correct? Also, I would try placing semicolons at the end of your javascript statements to be a little more syntaxically (is that a word?) sound. Are you getting any error messages from your browser? If so, what are they saying? Perhaps also try declaring all those variables you're using too.

  3. #3
    SitePoint Addict lundberg's Avatar
    Join Date
    Mar 2003
    Location
    Sweden
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed it, the problem was I used both firstname and firstnamn =) thansk for the answer anyway!

    Martin Lundberg
    Sweden


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
  •