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 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem combining this code into one function

    I need to combine these three functions into one function so that it will work on the form when the submit button is clicked. I can get each function to work individually but I get errors when I try to use all of them. I have tried several different ways (guesses) to try and combine them into one function but my lack of javascript knowledge only creates frustration and more errors. Any help is appreciated.

    function IGI_validate()
    {

    if ( (document.subscription.Name.value == '')
    || (document.subscription.Email.value == '')
    || (document.subscription.EmailConfirm.value == ''))
    {
    alert("Please fill out the form fields completely.")
    return false
    }
    }

    function emailcheck() {
    var e=document.subscription.Email.value
    if ((e.indexOf("@")==-1)||
    (e.indexOf(".")==-1))
    {
    alert("Please input a valid email address!")
    document.subscription.Email.focus()
    }
    }

    function emailsMatch(document.subscription.Email.value,document.subscription.EmailConfirm.value)
    {
    if (document.subscription.Email.value != document.subscription.EmailConfirm.value)
    {
    alert("You're Email Addresses do not match!");
    return false;
    }
    return true;
    }

    Thanks for your help!

  • #2
    Super Moderator sage45's Avatar
    Join Date
    May 2002
    Posts
    1,060
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Try this:
    Code:
    function validate(){
    
    var e=document.subscription.Email.value
    if ((e.indexOf("@")==-1)||
    (e.indexOf(".")==-1))
       {
       alert("Please input a valid email address!")
       document.subscription.Email.focus()
       }
    
    if ((document.subscription.Name.value == '') || (document.subscription.Email.value == '') || (document.subscription.EmailConfirm.value == ''))
       {
       alert("Please fill out the form fields completely.")
       return false
       }
    
    if (document.subscription.Email.value != document.subscription.EmailConfirm.value)
       {
       alert("You're Email Addresses do not match!");
       return false;
       }
    
    return true;
    }
    HTH,

    -sage-
    HTML & CSS Forum Moderator

    "If you don't know what you think you know, then what do you know."
    R.I.P. Derrick Thomas #58
    1/1/1967 - 2/8/2000

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Save yourself from typing.
    Code:
    function validate(f){
    
    var e=f.Email.value
    if ((e.indexOf("@")==-1)|| (e.indexOf(".")==-1))
       {
       alert("Please input a valid email address!")
       f.Email.focus()
       return false;
       }
    
    if ((f.Name.value == '') || (f.Email.value == '') || (f.EmailConfirm.value == ''))
       {
       alert("Please fill out the form fields completely.")
       return false
       }
    
    if (f.Email.value != f.EmailConfirm.value)
       {
       alert("You're Email Addresses do not match!");
       return false;
       }
    
    return true;
    }
    ...
    <form onsubmit="return validate(this)">
    ...
    <input type="submit" />
    </form>


  •  

    Posting Permissions

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