Results 1 to 3 of 3

Thread: Help!

  1. #1
    New to Java Script Guest

    Default Help!

    I am new to Java Script and I am having some trouble writing good code.<BR><BR>I am using Java Script to validate a form by using the OnClick:<BR><BR>&#060;input type="submit" value="Apply" id=submit onClick="return CheckInput();"&#062;<BR><BR>CheckInput is a long Java Script Function that checks the values of each field in the form. Like:<BR><BR>// Check to see if Customer Name is Empty<BR>if(document.Users.FirstName.value=="")<BR >{<BR>alert(&#039You have not entered a FIRST NAME in this form.&#039); document.Users.FirstName.focus(); return(false);<BR>}<BR><BR>// Check to see if Customer Name is Empty<BR>if(document.Users.LastName.value=="")<BR> {<BR>alert(&#039You have not entered a LAST NAME in this form.&#039); document.Users.LastName.focus(); return(false);<BR>}<BR> <BR>// Check to see if Address is Empty<BR>if((document.Users.Address1.value=="") && (document.Users.Address2.value==""))<BR>{<BR>alert (&#039You have not entered a MAILING ADDRESS in this form.&#039);document.Users.Address1.focus(); return(false);<BR>}<BR> <BR>// Check to see if City is Empty<BR>if(document.Users.City.value=="")<BR>{<BR >alert(&#039You have not entered a CITY in this form.&#039); document.Users.City.focus(); return(false);<BR>}<BR><BR>As you can see, the more fields in my form, the more I am repeating my code when I do something like check to see if a field is empty. How can I change what I am doing to make it more efficient and so I am reusing more code?

  2. #2
    hermida Guest

    Default RE: Help!

    here&#039s a thought... maybe it&#039s not a good thought... but it&#039s a thought...<BR><BR>how about if you just do something along the lines of <BR><BR>if(document.Users.LastName.value=="")<BR > OR<BR>if(document.Users.LastName.value=="")<BR><BR >.<BR>.<BR>.<BR><BR>then <BR>alert("One or more required fields are empty. Please go back and make sure you&#039ve filled in all fields.")

  3. #3
    N2JS Guest

    Default RE: Help!

    It is not a bad thought! I was hoping more for something where I could do something like:<BR><BR>for [a statement that will loop through each item in the form]<BR>document.Admin.[variable or something].value==""<BR>{<BR>alert(&#039The field &#039+somevar+&#039 is blank. Fix it...&#039);return (false);<BR>}<BR><BR>But maybe your way is the best way! Thanks for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts