SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Hybrid View

  1. #1
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript checking email address

    I need a simple js function that will check if the value (an email address) in two text fields match.

    If someone enters creole@creole.com in "fieldOne" and creoel@creole.com in field two it would throw an error. If they matched, the form would submit silently.

    Can someone help me out? I think it should be easy, and I'm trying to do it myself, I'm just not good enough at js.
    Last edited by creole; May 1, 2002 at 13:07.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  2. #2
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hope this helps

    PHP Code:
    <script type="text/javascript">

        function 
    checkIT()
        {
            if (
    document.form1.text1.value == document.form1.text2.value) {alert('same'); return false;}
            return 
    true;
        }

    </script>
     
    <form name="form1" method="get" action="_self" onsubmit="return checkIT();">
      <input type="text" name="text1">
      <input type="text" name="text2">
      <input type="submit" value="submit">
    </form> 
    document.form1.text1.value.indexOf("@");

    check for email validity with that
    Last edited by Andrew-J2000; May 1, 2002 at 13:16.

  3. #3
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow...that was fast. It doesn't seem to work though. Here's the code I have:

    <script type="javascript">
    function checkIt() {
    if (document.form1.fieldOne.value == document.form1.fieldTwo.value) {alert('same'); return false;}
    return true;
    }
    </script>
    <body>

    <form name="form1" method="get" onsubmit="checkIt();">
    Field One: <input type="text" name="fieldOne" size="40" value="">
    <br><br>
    Field Two: <input type="text" name="fieldTwo" size="40" value="">
    <br><br>
    <input type="submit" value="submit">
    </form>

    Using that, it throws a javascript error on line 14 which is the FORM tag line. Did I leave something out?
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  4. #4
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <onsubmit="checkIt();">

    your trying to return an event to cancel onsubmit otherwise it will continue to post the form even if you do return false with in the function

    <onsubmit="return checkIT()">

    PHP Code:
    <script type="text/javascript">

        function 
    checkIT()
        {
            if (
    document.form1.fieldOne.value == document.form2.fieldTwo.value) {alert('same'); return false;}
            return 
    true;
        }

    </script>

    <body> 

    <form name="form1" method="get" onsubmit="return checkIT()"> 

    Field One: <input type="text" name="fieldOne" size="40" value="" /><br /><br />
    Field Two: <input type="text" name="fieldTwo" size="40" value="" /><br /><br />
           <input type="submit" value="submit" />
    </form> 

  5. #5
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...no js error but now it simply clears the form when I press submit. I made sure to enter two values that don't match but I get no alert box.

    By the way, before I ask any more questions...thanks for your help. I appreciate it.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, np, you might kick yourself for this

    the reason it clears the form is because you havent set an action attribute in the form lol.

    PHP Code:
    <script type="text/javascript">

        function 
    checkIT()
        {
            if (
    document.form1.fieldOne.value == document.form2.fieldTwo.value) {alert('same'); return false;}
            return 
    true;
        }

    </script>

    <body> 

    <form name="form1" method="get" action="http://www.sitepoint.com" onsubmit="return checkIT()"> 

    Field One: <input type="text" name="fieldOne" size="40" value="" /><br /><br />
    Field Two: <input type="text" name="fieldTwo" size="40" value="" /><br /><br />
           <input type="submit" value="submit" />
    </form> 
    theres a bug just fixing now i just saw

  7. #7
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i forgott to change the form name in the if statement to form1 thats all. works now

    PHP Code:
    <script type="text/javascript">

        function 
    checkIT()
        {
            if (
    document.form1.fieldOne.value == document.form1.fieldTwo.value) {alert('same'); return false;}
            return 
    true;
        }

    </script>

    <body> 

    <form name="form1" method="get" action="http://www.sitepoint.com" onsubmit="return checkIT()"> 

    Field One: <input type="text" name="fieldOne" size="40" value="" /><br /><br />
    Field Two: <input type="text" name="fieldTwo" size="40" value="" /><br /><br />
           <input type="submit" value="submit" />
    </form> 

  8. #8
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK...

    It does go to SitePoint when I submit now, but it still shows no error when values are different.

    I enter this in field One: creole@creole.com
    and
    this in field two: creole@creole.cmo

    When I press submit it simply goes to SitePoint without error.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  9. #9
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here you go

    http://andrew-j.port5.com/creole.html

    oh:O

    so you want it to just check if it has @creole.com then

    as

    I enter this in field One: creole@creole.com
    and
    this in field two: creole@creole.cmo
    Last edited by Andrew-J2000; May 1, 2002 at 14:10.

  10. #10
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Give this a try Andy:
    Code:
    <script language="javascript" type="text/javascript" />
    <!--
    function validate(theform){
    	if(theform.fieldOne.value != theform.fieldTwo.value){
    		alert("Please ensure the e-mail addresses match.");
    		return false;
    	}
    }
    //-->
    </script>
    
    ...
    
    <form name="form1" method="get" action="http://www.sitepoint.com/" onsubmit="return validate(this)">
    Field One: <input type="text" name="fieldOne" size="40" /><br /><br />
    Field Two: <input type="text" name="fieldTwo" size="40" /><br /><br />
        <input type="submit" name="submit" value="submit" />
    </form>


    -Colin

    Edit: You should make sure that you remove the value="" from the input fields as that can sometimes have adverse effects on the operation of such a script as this.
    Last edited by Aes; May 1, 2002 at 14:16.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  11. #11
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope...

    I just want to make sure that any email address entered in fieldTwo matches the email address entered in fieldOne. It doesn't even have to be email addresses actually. You can just use "any value".
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  12. #12
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol wish i read the first post properly lol.

    sry for the messing about lol

  13. #13
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought you wanted an alert when they were the same hehe, it is 5:13am and i havent slept yet so you can't blame me:P

  14. #14
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's all practice anyway. I still thank you for your willingness to help.

    Colin...

    I get an error on line 24 which is the FORM tag.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  15. #15
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am receiving no error. Give me a moment.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  16. #16
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  17. #17
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works perfectly for me in Mozilla and Opera. Thanks for the "demo" Andrew.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  18. #18
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just tried Andrew's demo and I get no alert box. It's supposed to throw an error if the values don't match. I entered two values that don't match and the form simply submits like normal.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  19. #19
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what browser are you using?

    try this

    PHP Code:
    <script type="javascript">
    <!--
            function 
    validate(formObj)
            {
                    if (
    formObj.temp.value == '' || formObj.temp.value == null)
                    {                
                            
    alert ("Your field is empty");
                            return 
    false;
                    }
                    else
                    {
                            
    submitform()
                    }
            }
            function 
    submitform()
            {
                
    With (document.form1)
                {
                    
    action "http://sitepointforums.com";
                    
    method "get";
                    
    enctype "application/x-www-form-urlencoded";
                    
    target "_self"
            
                    
    submit(); 
                }
                return 
    true;
            } 
    //-->
    </script>
    </head>

    <body>
    <form Name="form1" id="form1" onsubmit="return validate(this)">
    <Input Type="text" Name="temp">
    <Input Type="submit" Name="submit" value="submit">
    </form> 

  20. #20
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa, whoa, whoa. Not to be stingy here, but I tested my example that Andrew linked to in Mozilla 0.9.9, Opera 6.01, and Internet Explorer 6.0. All of them work wonderfully and return no errors when the fields match and alert me when the fields do not match. What browsing client are you using creole? It works for both Andrew and I.

    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  21. #21
    SitePoint Wizard creole's Avatar
    Join Date
    Oct 2000
    Location
    Nashvegas Baby!
    Posts
    7,845
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IE 5.5.

    Tried it here at home and get the same thing. I enter biteme@biteme.com in the first field and creole@creole.com in the second field, hit submit and it goes straight to sitepoint without a whimper.
    Adobe Certified Coldfusion MX 7 Developer
    Adobe Certified Advanced Coldfusion MX Developer
    My Blog (new) | My Family | My Freelance | My Recipes

  22. #22
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hmm.. I'm using IE 5.5

    I tried different and same entries and got a line 14 error, but was forwarded to SP both times
    Mike
    It's not who I am underneath, but what I do that defines me.


Bookmarks

Posting Permissions

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