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 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Comparing two fields - please help!

    Hi there,

    I am trying to write javascript that compares and validates that two form fields are identical. But I can't make it work in what I've already typed in for my form. Any suggestions? Here is my form code:


    ____________________________________________________________
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE>Contact Form</TITLE>

    <SCRIPT LANGUAGE="JavaScript">
    <!--


    var error = new createerror();
    var errors = new Array();
    errors[31] = "Illegal character in a numeric input";
    errors[64] = "The field is empty.";
    errors[131] = "Illegal character. You can only enter letters and blank spaces.";
    errors[150] = "Please input a valid email address.";
    errors[151] = "Please input a valid ten digit number.";
    errors[152] = "The email addresses are not the same.";

    var alphachars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var emailFilter=/^.+@.+\..{2,3,4,6}$/;
    var digits = "0123456789";





    function createerror() {
    this.val = 0;
    return this;
    }
    function validateForm() {
    var correct;
    correct = checkblank(document.myForm.name.value, error);
    if (!correct) {
    alert('Name: ' + errors[error.val]);
    document.myForm.name.focus();
    return false;
    }
    correct = checkalpha(document.myForm.name.value, error);
    if (!correct){
    alert('Name: ' + errors[error.val]);
    document.myForm.name.focus();
    return false;
    }

    correct = checkblank(document.myForm.lastname.value, error);
    if (!correct) {
    alert('Last Name: ' + errors[error.val]);
    document.myForm.lastname.focus();
    return false;
    }
    correct = checkalpha(document.myForm.lastname.value, error);
    if (!correct){
    alert('Last Name: ' + errors[error.val]);
    document.myForm.lastname.focus();
    return false;
    }




    correct = checkblank(document.myForm.email.value, error);
    if (!correct) {
    alert('Email: ' + errors[error.val]);
    document.myForm.email.focus();
    return false;
    }
    correct = checkemail(document.myForm.email.value, error);
    if (!correct) {
    alert('Email: ' + errors[error.val]);
    document.myForm.email.focus();
    return false;
    }




    correct = checkblank(document.myForm.confirmemail.value, error);
    if (!correct) {
    alert('Confirm Email: ' + errors[error.val]);
    document.myForm.confirmemail.focus();
    return false;
    }
    correct = checkemail(document.myForm.confirmemail.value, error);
    if (!correct) {
    alert('Confirm Email: ' + errors[error.val]);
    document.myForm.confirmemail.focus();
    return false;
    }

    correct = checkblank(document.myForm.phone.value, error);
    if (!correct) {
    alert('Phone: ' + errors[error.val]);
    document.myForm.phone.focus();
    return false;
    }
    correct = checkphonenumber(document.myForm.phone.value, error);
    if (!correct) {
    alert('Phone: ' + errors[error.val]);
    document.myForm.phone.focus();
    return false;
    }

    correct = checkblank(document.myForm.fax.value, error);
    if (!correct) {
    alert('Fax: ' + errors[error.val]);
    document.myForm.fax.focus();
    return false;
    }
    correct = checkphonenumber(document.myForm.fax.value, error);
    if (!correct) {
    alert('Fax: ' + errors[error.val]);
    document.myForm.fax.focus();
    return false;
    }



    return true;
    }

    function checkblank(fieldValue,error)
    {
    if (fieldValue.length == 0) {
    error.val = 64;
    return false;
    }
    for (var i = 0; i<fieldValue.length; i++) {
    if (fieldValue.charAt(i) != ' ' && fieldValue.charAt(i) != '\t') {
    return true;
    }
    }
    error.val = 64;
    return false;
    }
    function isalpha(sChar){return (alphachars.indexOf(sChar) >= 0)}

    function checkalpha(fieldValue,error)
    {
    for (var i = 0; i<fieldValue.length;i++)
    {
    if (!isalpha(fieldValue.charAt(i))){
    error.val = 131;
    return false;
    }
    }
    return true;
    }


    function checkemail(){
    var str=document.myForm.email.value
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
    testresults=true
    else{
    error.val = 150;
    return false;
    }
    return true;
    }

    function checkconfirmemail(){
    var str=document.myForm.confirmemail.value
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
    testresults=true
    else{
    error.val = 150;
    return false;
    }
    return true;
    }




    function checkphonenumber(str){
    var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
    if (re.test(str))
    testresults=true
    else{
    error.val = 151;
    return false;
    }
    return true;

    }






    // -->
    </SCRIPT>

    </HEAD>

    <BODY bgcolor="#000000"><font color="#ffffff">
    <center>
    <H2>Contact Us</H2>
    <h4>*Indicates a required field</h4>
    <FORM NAME="myForm" METHOD="post"
    ACTION="http://free.allforms.mailjol.net/u/d0ac0bdf.php">
    *Name:<BR>
    <INPUT TYPE="text" size="30" NAME="name" ><br>
    *Last Name:<BR>
    <input type="text" size="30" name="lastname"><br/>
    Street Address:<br />
    <input name="Street" type="text" size="30" />
    <br/>
    Unit#:<br/> <input name="Unit" type="text" size="5" />
    <br />
    City:<br /> <input name="City" type="text" size="30" />
    <br/>
    Postal Code: <br /><input name="PostalCode" type="text" size="7"/>
    <br/>
    *Email address:<BR>
    <INPUT TYPE="text" size="30" NAME="email"><br>
    *Confirm Email address:<BR>
    <INPUT TYPE="text" size="30" NAME="confirmemail"><br>
    *Phone number:<BR>
    <INPUT TYPE="text" size="30" NAME="phone"><br>
    Mobile number:<BR>
    <INPUT TYPE="text" size="30" NAME="mobile"><br />
    Work Phone:<br/>
    <input name="Work" type="text" size="20" maxlength="12" /><br />
    Comments: <br/>
    <textarea name="Comments" rows="10" cols="50">Let us know what you think!</textarea>
    <br />


    <INPUT TYPE="submit" VALUE="Submit Data" onClick="javascript:return validateForm();">
    <INPUT TYPE="reset" VALUE="Reset Form">
    </FORM>

    </font>

    </BODY>
    </HTML>

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by Vanbot View Post
    But I can't make it work in what I've already typed in for my form.
    It would be quite helpful to know what is failing for you. A quick test here allows submission but with this error:
    document.myForm.fax has no properties
    Line 104

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Oh, yes sorry about that, I'm in the middle of adding a fax part to the form.

    I just need to know how to compare the two email fields.

    ~Vanessa

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    They're just strings, so all you need to do is check for ==.

  • #5
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I'm not sure what the correct way to check for == is? All the code I have been writing isn't working

    ~Vanessa

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    The correct way is the same way you've done it in other if statements within the same code, like here:
    Code:
    if (fieldValue.length == 0) {
    error.val = 64;
    return false;
    }
    You're comparing fieldValue.length and 0 for == there. Do the same thing with your strings.

  • #7
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Will this help me compare if email and confirmemail are the same?

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    If you modify it, yes.

    I find it hard to believe that you've written the rest of the code, but are having difficulty with this part. Is this someone else's code that your working with? Is it a homework assignment?

  • #9
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    It is for a homework assignment, I wrote the code with the help of my school textbook but can't figure out how to compare two fields. I have tried:

    correct = checkemailmatch(document.myForm.email.value, error);
    if (!email==confirmemail) {
    alert('Emails: ' + errors[152]);
    document.myForm.email.focus();
    return false;
    }

    But it doesn't work

  • #10
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Ok, good. Now I understand better and should be able to point you in the right direction.

    Your last code is close, but not quite there yet. First, notice that you're calling a function checkemailmatch here:
    Code:
    correct = checkemailmatch(document.myForm.email.value, error);
    But that function is not in your original code. You're on the right track, though, in that you'll probably want to use a function because all of the rest of the validation steps use functions. We'll get back to the function in a minute.

    This line:
    Code:
    if (!email==confirmemail) {
    is almost the correct way to check whether the fields are equal, but that test should be inside the new checkemailmatch function, and the variable names need to be a little different. If that test is true (if the emails don't match), then you'll want to set the error.val and return false - just like in the other validation functions. If the test is false (if the emails do match), you should return true like in the other functions.

    But the original function call you have listed is not correct. This one:
    Code:
    correct = checkemailmatch(document.myForm.email.value, error);
    There are a couple of ways to do it, but the easiest (and I think the one that is most in line with the rest of the code) is to only pass one parameter - error. Like this:
    Code:
    correct = checkemailmatch(error);
    So that implies that your new function definition should look like this:
    Code:
    function checkemailmatch(error)
    {
      ...more code here
    And the first thing you want to do in the new function is to get the values that are in the form. I won't show how to do that because you have the code already as the first line in the functions checkemail and checkconfirmemail - just rename the variables so they're different. Then you use the new variable names in the if statement we talked about earlier, such as:
    Code:
    if (!asmellyredmonkey==cheeseburgerscanfly)
    /*assuming you have used these bad variable names*/
    And the rest of the function we've already talked about.

    Now, back up after your new function call (the one that will look like this):
    Code:
    correct = checkemailmatch(error);
    Following that, the code should be easy to figure out because you still want to check if not correct and still set and alert and set focus if that's the case - almost the same as everything else.

    That should be enough information for you to complete the work and understand what's happening.

  • #11
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    So...this is what I've come up with, but still isn't working Any suggestions?

    ___________________________________________


    function checkemailmatch(error)


    correct = checkemailmatch(error);
    {
    if (email==confirmemail)
    testresults=true
    else{
    error.val = 152;
    return false;
    }
    return true;
    }

  • #12
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    That's close, but still not quite there. You're piling the function and the function call all together. Look back at the rest of the code and see how the functions are set up and how the function calls and result tests are set up. Hint: The call/test sections begin with correct = something. The functions are obvious. Also in your code, you're still not fetching the values from the form. My previous post explains that.

  • #13
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    ok I've separated them, like you said, but now my entire form isn't working, I'm sorry I'm trying but I just don't understand this.


    function checkemailmatch(error){
    var str=document.myForm.email.value
    var str2=document.myForm.confirmemail.value
    }


    correct = checkemailmatch(error);
    {
    if (str==str2)
    testresults=true
    else{
    error.val = 152;
    return false;
    }
    return true;
    }

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    I'm not sure what the correct way to check for == is?
    I do get the impression that you are trying to run before you can walk.

    Code:
    function checkemailmatch(error) {
    var str = document.myForm.email.value;
    var str2 = document.myForm.confirmemail.value;
    if (str == str2) {
    testresults = true;
    }
    else {
    errorval = 152;
    return false;
    }
    }
    Quizmaster: What three-letter word means "at this moment"?
    Contestant: Then.
    Last edited by Philip M; 06-11-2008 at 07:41 AM.


  •  

    Posting Permissions

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