Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,338 Programmers for FREE! Ask your question and get quick answers from experts. There are 935 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Form Validation

 
Reply to this topicStart new topic

Form Validation

Randall
post 18 Jun, 2008 - 08:37 PM
Post #1


New D.I.C Head

*
Joined: 26 Mar, 2008
Posts: 19

Hi Guys,

im trying to get my form to validate i have it all set out but it just dosnt seem to be validating, would anyone be able to take a look at my code and point me in the right direction?

here is my java script:
CODE

<script>

function Validate(){

var Name = document.myform.firstname.value
var Surname = document.myform.surname.value
var Suburb = document.myform.Suburb.value
var Address = document.myform.address.value
var DOB = new Date(document.myform.DOB.value)
var Postcode = document.myform.Postcode.value
var Phone = document.myform.Phone.test.value
var Email = document.myform.Email.value
var Username = document.myform.Username.value
var Password = document.myform.pass1.value
var Retype = document.myform.pass2.value

document.myform.Name.select



if (Name == ""){
    alert("Name required")
    document.myform.firstname.select.focus()
    return false
}

if (Surname == ""){
    alert("Surname required")
    document.myform.surname.select.focus()
    return false
}

if (document.myform.address.value) {
        alert ("Enter a suburb")
        document.myform.address.select.focus()
        return false
    }

    if (document.myform.suburb.value) {
        alert ("Enter a suburb")
        document.myform.suburb.select.focus()
        return false
    }

else if (DOB == "NaN") {
    alert("DOB required in the format DD/MM/YYYY")
    document.myform.DOB.select.focus()
    return false
}



else if (Postcode.length != 4){
    alert("Postcode Required");
    document.myform.Postcode.select.focus()
    return false
}

else if (isNaN(Postcode) == true){
    alert("Postcode must be a four digit number.");
    document.myform.Postcode.select.focus()
    return false
}

function isPhoneNumber(s)
{


     Phone = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

     if (!Phone.test(s)) {
          alert("Phone Number Must Be Entered As: (555) 555-1234");
          return false;
     }

return true;
}

else if (Email.indexOf("@") == -1){
    alert("Valid Email Address Required");
    document.myform.Email.select.focus()
    return false
    }
    function checkData() {
      if (document.form.username.value.length <= 0) {
        alert ("Enter a Username.");
        document.myform.username.focus();
        return false
    }
    if (document.myform.pass1.value.length.select.focus < 20) {
        alert ("Enter a password at least between 4 and 20 characters long")
        return false
    }
    if (document.myform.pass2.value != document.myform.pass2.value.select.focus) {
        alert ("Your confirmed password does not match the entered password")
        return false
}
}
</script>


and here is my html:
CODE

<body>


<h1>Form Validation</h1>

<form name="myform" onSubmit="return Validate()">

Name:<input type=text name="firstname"><br>

Surname:<input type=text name="surname"><br>

Address:<input type=text name="address"><br>

Suburb:<input type=text name="suburb"><br>  

Date of Birth (DD/MM/YYYY): <input type="text" name="DOB"> <br>

Postcode: <input type="text" name="Postcode"><br>

Phone: <input type=text name="phone"><br>

Email: <input type="text" name="Email"><br>

Username: <input type="text" name="Username"><br>

Password: <input type="text" name="pass1"><br>

Retype: <input type="text" name="pass2"><br>

<input type="submit" value="Submit"><br>
<input type="reset" value="Reset">                

</form>
</body>


Thanks in advance!

Regards

Randall.
User is offlineProfile CardPM

Go to the top of the page


Mike007
post 18 Jun, 2008 - 11:00 PM
Post #2


D.I.C Head

Group Icon
Joined: 30 Aug, 2007
Posts: 200



Dream Kudos: 75
My Contributions


why is there a function in the middle of your if else structure? And i would check your logic with the if statements, looks a bit off. Ok on second thought looks wrong... A function inside a function?

Seems to me that you didn't really think this code through before you started coding. please do and then take a look at it again.

Just one more thing i wanted to add, be more persistent with the name you pick, either capitalize the first letter in all the name or leave it lower case in all of them. It's just so that you won't get confused later on when you are writing your code and think, "did i capitalize this? or maybe i called it txt instead of text". And please for the love of god, javascript lets you get away with not using a ; but please use it at the end of every statement anyway, just a good practice.

This post has been edited by Mike007: 18 Jun, 2008 - 11:23 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/6/08 04:55PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month