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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can anyone plz help me correct my form validation Javascript plz ?

    I have the following Java codes:

    function validate_form ( )
    {
    valid = true;

    if ( enquire.txtname.value == "" )
    {
    alert ( "Please fill in the ' Name' box." );
    valid = false;}



    if ( enquire.txtemail.value == "" )
    {
    alert ( "Please fill in the 'Email' box." );
    valid = false;}


    if ( enquire.txtphone.value == "" )
    {
    alert ( "Please fill in the 'Phone' box." );
    valid = false;}


    if ( enquire.txtsubject.value == "" )
    {
    alert ( "Please fill in the 'Subject' box." );
    valid = false;}

    if ( enquire.txtmessage.value == "" )
    {
    alert ( "Please fill in the 'Message' box." );
    valid = false;}
    return valid;
    }

    The purpose of this code is to validate the textboxes when the submit button is click; to see whether they are empty or not. If it is empty, a message box will come up and say that field is empty.

    I can run this code alright, but it doesn't quite do waht I really want it to do. If the user leave all 5 fields empty and lcik the submit button, five message box will show up. I don't want this to happen.

    The way I want it to work is if the user doesn't input anything into the 5 field, the first field ( in my case, it is the name fireld) message box will come only. If the name field is filled in and the other four aren't, the "email" message box will come up. The rest follows this logic

    Can some experts help me correcting my code plz

    Thx

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,950
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Make the 2nd to the last if statements as else if.

    Code:
    else if ( enquire.txtemail.value == "" ){
    
    }
    else if ( enquire.txtphone.value == "" ){
    
    }
    ...

  • #3
    New to the CF scene
    Join Date
    Mar 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thx alot gleen

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Another way is to put a
    Return false
    everytime the conditions are not met like...

    Code:
    function validate_form ( )
    {
    valid = true;
    
    if ( enquire.txtname.value == "" )
    {
    alert ( "Please fill in the ' Name' box." );
    valid = false;}
    return false
    }
    And put this attribute in your form tags
    Code:
    onSubmit="return validate_form();"
    I prefer doing it that way because then the javascript exits immediately a condition is not met and doesn't have to go through the whole code every time.
    Last edited by tboss132; 03-24-2005 at 08:02 AM.
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #5
    New Coder
    Join Date
    Oct 2002
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    my way of form validation

    function validateForm(myForm){
    var msg = "";
    if(myForm.myTextBox1.value.length < 1){
    msg += "please provide an appropriate value.\n";
    }elseif(myForm.myTextBox2.value.length < 2){
    msg += "please enter a value! \n";
    }

    if (msg.length >0){
    alert(msg);
    return false;
    }else{
    return true;
    }
    }

    <form name="myForm" onsubmit="return validateForm(document.myForm);">

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hope this might be informative

    function chk(f){

    if (f.enq_name.value==""){
    f.enq_name.focus();
    alert ('Please enter your Name');
    return false;
    }
    similair ifs for other texts validations
    //


    }

    you can invoke this script function with the following

    onClick=\"JavaScript:return chk(this.form);\"

    happy to share with you and this should help you...wishing you the best


  •  

    Posting Permissions

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