Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Validation Without alert()

    Hi,

    I have the following code somewhat working:

    Code:
    function customerAdd(form){
    	if (form.email.value == ""){
    		var email = document.getElementById("email");
    		email.style.color = "red";
    	}
    	if(form.isActive[0].checked == false && form.isActive[1].checked == false){
    		var isActive = document.getElementById("isActive");
    		isActive.style.color = "red";
    	}
    	if(form.receiveEmails[0].checked == false && form.receiveEmails[1].checked == false){
    		var receiveEmails = document.getElementById("receiveEmails");
    		receiveEmails.style.color = "red";
    	}
    	if (form.salutation.value == "" || form.salutation.value == "- Please Select -"){
    		var salutation = document.getElementById("salutation");
    		salutation.style.color = "red";
    	}
    	return false;
    }
    What I meant by somewhat working was that the code actually changes the color of the selected "id" when the fields are empty or have not been selected, but when all the required fields are selected the form doesn't do anything. I knwo it has to do with the return false;

    How can I get this to work so that if the user doesn't fill out the required fields they would be highlighted and if everything is filled out the form will submit? I had the code working before with alert boxes, but I wanted to do something different with the way the validation worked rather than having the alert box pop up for each missed field.

    I appreciate any and all help!

  • #2
    New Coder
    Join Date
    Jul 2006
    Posts
    45
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I also wanted to know how can you check to see if a form field is in the form. I have several pages of forms for users to enter data and I wanted to reuse the same validation code for all my pages rather than creating separate functions for each page.

    Let's say based on the 4 form fields on my previous post, page1.cfm uses all 4 fields and page2.cfm only uses 2 of the fields, how can i use that same validation for both pages without having to create separate functions for each page?

  • #3
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Post the html you are using please.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by 7079 View Post
    I knwo it has to do with the return false;
    As Aero said, without seeing your html it is guessing, but maybe try this for your first question.

    Code:
    function customerAdd(form){
      var noErrors = true;
      if (form.email.value == ""){
        var email = document.getElementById("email");
        email.style.color = "red";
        noErrors = false;
      }
      if(form.isActive[0].checked == false && form.isActive[1].checked == false){
        var isActive = document.getElementById("isActive");
        isActive.style.color = "red";
        noErrors = false;
      }
      if(form.receiveEmails[0].checked == false && form.receiveEmails[1].checked == false){
        var receiveEmails = document.getElementById("receiveEmails");
        receiveEmails.style.color = "red";
        noErrors = false;
      }
      if (form.salutation.value == "" || form.salutation.value == "- Please Select -"){
        var salutation = document.getElementById("salutation");
        salutation.style.color = "red";
        noErrors = false;
      }
      return noErrors;
    }
    david_kw


  •  

    Posting Permissions

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