Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006
    Posts
    1

    Unanswered: asp/html comments form

    Hi everyone,
    I would be really grateful if someone could help me here, I've recently put a comments form on my website but i've been trying to find out how to add validation to the input boxes and its giving me a headache as i'm not very good with asp. My form is located at http://www.galaxywindows.co.uk/contact/form.html

    but when the submit button is pressed even if no data is entered it still says message sent and I recieve it in my inbox. could someone help me with fixing this as I'm not really sure how.

    Many thanks

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Paul

  3. #3
    Join Date
    Jul 2006
    Posts
    56

    Validating data in ASP or with JavaScript

    There are two ways you can validate data from a ASP based form. Either you can use the next page to identify the error and then send the user back to the form page where you should let the user know which field he got it wrong, OR you can use JavaScript to valida your form on submittion.

    Here, lemme give you an example based on your simple form:

    Code:
    function validateForm(form) {
    	this.form = form;
    	errors = 0
    	
    
    /* ------------------------------------------
    form contact
    ------------------------------------------ */
    
    if (form == "contact") {
    
    form = document.contact;
    
    /* email*/
    
    if (errors== 0) {
    
    Ctrl = form.email;
    
    if ((Ctrl.value == "" || Ctrl.value.indexOf('@', 0) == -1) || Ctrl.value.indexOf('.') == -1) {
    
    validatePrompt(Ctrl, "Please, type in a valid e-mail adress.");
    errors++;
    }
    }
    
    
    /* name */
    
    if (errors == 0) {
    
    Ctrl = form.name;
    
    if (Ctrl.value == "") {
    
    validatePrompt(Ctrl, "Please, type in your name.");
    errors++;
    
    }
    }
    
    
    /* comments */
    
    if (errors == 0) {
    
    Ctrl = form.comments;
    
    if (Ctrl.value == "") {
    
    validatePrompt(Ctrl, "Please, type in your comments.");
    errors++;
    
    }
    }
    
    
    /* ------------------------------------------
    true or false
    ------------------------------------------ */
    
    if (errors > 0) {
    return false;
    } else return true
    
    }
    
    
    /* ------------------------------------------
    Focus and Error Alert
    ------------------------------------------ */
     function validatePrompt(Ctrl, PromptStr) {
    	alert (PromptStr);
    	
    	if (Ctrl == "NO") {
    		return;
    	} else {
    		Ctrl.focus();
    		return;
    		}
     }
    
    // -->
    Name your form "contact", and make sure you name your fields correctly too.

    Also, notice that you may use "NO" instead of Ctrl in the ValidatePrompt Function if you don't want this script to focus on a certain field (like, you wouldn't want to do that if the field were a radio...)

    Your form tag should look like this:
    Code:
    <form name="contact" action="next_asp_file.asp" method="post" target="if any" onSubmit="return validateForm(this.name);" >
    Lemme know if this helped.

Posting Permissions

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