Advanced Search

Results 1 to 3 of 3

Thread: Compare input fields

  1. #1
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Compare input fields

    Hi,
    New to JavaScript is an understatement but here goes. Sorry about the "{}" thingies but I put them together so I could stay up with them. I would appreciate any help if someone would be so kind.

    My goal is to compare e-mail addresses to make sure they are the same and to make sure the fields are not mt. I ended up with two script sections in the head for my best results. It works except after the "The e-mail fields aren't the same!!" alert is clicked it goes ahead and submits the form. This is making my head hurt after two days on it. Thanks


    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    function validate_required(field,alerttxt)
    {with (field)
    {if (value==null||value=="")
      {alert(alerttxt);return false}
    else {return true}}}
    function validate_form(thisform)
    {with (thisform)
    {if (validate_required(mail,"Please fill out your name and e-mail address so I can contact 
    
    you")==false)
      {mail.focus();return false}}}
    </script>
    
    
    
    
    <script type="text/javascript">
    function check(a,b) {
    var obja = document.getElementById(a)
    var objb = document.getElementById(b)
    if (obja.value==objb.value) {}
    else {alert("The e-mail fields aren't the same!!");return false;}}
    
    </script>
    
    
    
    </head>
    <body>
    
    <form action="gdform.asp" onsubmit="return validate_form(this)"method="post">
    <input type="hidden" name="subject" value="Form Submission" /> 
    <input type="hidden" name="redirect" value="thankyouredriver.html" />
    <h4>Use this form to send Mike feedback.</h4>
    Your Name:<br>
    <input type="text" name="name">
    <br><BR><br>
    Your E-Mail address:<br>
    <input type="text" name="mail">
    <br><br>
    Confirm E-Mail address:<br>
    <input type="text" name="mail2">
    <br><br>
    <input type="submit" onclick="check('mail','mail2')"name="submit" value="Submit"/> 
    <input type="reset" value="Reset">
    </form>
    
    </body>
    </html>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,626
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Check the following code that achieves what you want I havent checked your code.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function formValidation()
    {
    	var email1 = document.getElementById("email1").value;
    	var email2 = document.getElementById("email2").value;
    	
    	var nameval = document.getElementById("name").value;
    	
    	if(nameval == "" || email1 == "" || email2 == "")
    	{
    		alert('plz enter values in all the form fields');
    		return false;
    	}
    	
    	if(email1 == email2)
    	{
    		if(!validateEmail(email1))
    		{
    			alert('Not a valid email address');
    			return false;
    		}
    	}
    	else
    	{
    		alert('Email values doesn\'t match..');
    		return false
    	}
    	alert('success');
    	return true;
    }
    
    //Function for validating email
    function validateEmail(emailVal) 
    {
        var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    
        if (! emailVal.match(re)) 
        {
            return false;
        }
        return true;
    }
    </script>
    </head>
    <body>
    
    <form name="form1" method="get" action="" onsubmit="return formValidation();">
    
    Name
    <input type="text" name="name" id="name"><br>
    Enter Email
    <input type="text" name="email1" id="email1"><br>
    Reenter Email
    <input type="text" name="email2" id="email2"><br>
    <input type="submit" value="submit" name="submit">
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Jan 2007
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by codeexploiter View Post
    Check the following code that achieves what you want I havent checked your code.
    I modified my form code and it seems to work with your script code. It does exactly what I want. Thank you so much.

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
  •