1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Form Submission and Validation problems

Discussion in 'JavaScript' started by jasonxxx102, Oct 17, 2013.

  1. #1
    I want to know why I'm getting the error "uncaught referenceerror invalid left-hand side in assignment" in scripts.js: line 3
    1. Ok, so the requirements of the form are as follows:

    2. First name and Last name should only contain letters.

    3. The form validation shall check the Firstname and Lastname fields, if they equal the defined name and the school is equal to the defined school then you should display the requested logo on the form page and change the page background color to green.

    4. The form validation shall ensure that the email addresses entered by the user match and are valid email addresses.

    5. If they do not match you should show the user an error message and put the focus (this is a form property!) on that field so the user can correct their error.

    6. The form validation shall return focus to incorrectly completed form fields (or the first incorrect if there are more than one incorrectly complete fields).

    7. The form validation shall give the user feedback that their form has been completed incorrectly by giving the user an error message.

    8. The form validation shall change the background color of incorrectly completed fields to red.
    Here's the HTML Form:

    <form method="post" id="myForm" name="input">
            First name: <input type="text" id="fName" ></br>
            Last name: <input type="text" id="lName" ></br>
            Email: <input type="text" id="emailAddress" ></br>
            Repeat Email: <input type="text" id="emailAddress1" ></br>
            Phone Number: <input type="text" id="pNumber" ></br>
            <select>
                <option value="" disabled="disabled" selected="selected">Please select your school</option>
                <option value="1">Binghamton University</option>
                <option value="2">MIT</option>
                <option value="3">Harvard</option>
                <option value="4">Berkley</option>
            </select>
            </br>
            <input type="submit" onClick = "validate_form()">
            </form>
    HTML:
    Here's my Javascript code

    function validate_form(){
    
    if(document.getElementById('fName').value == "" += validName() = false){
        document.getElementById('fName').style.background-color = 'red';
        alert("First name must be filled out");
        return false;
    }
    else if(document.getElementById('lName').value == "" += validName() = false){
        document.getElementById('lName').style.background-color = 'red';
        alert("Last name must be filled out");
        return false;
    }
    else if(document.getElementById('pNumber').value == ""){
        document.getElementById('pNumber').style.background-color = 'red';
        alert("Phone Number name must be filled out");
        return false;
    }
    else if(document.getElementById('emailAddress').value == "" += validEmail() = false){
        document.getElementById('emailAddress').style.background-color = 'red';
        alert("Email must be filled out");
        return false;
    }
    else if(document.getElementById('emailAddress1').value == "" += validEmail() = false){
        document.getElementById('emaildAddress1').style.background-color = 'red';
        alert("First name must be filled out");
        return false;
    }
    else{
        changePage();
    }
    return false;
    }
    
    function changePage(){
    
    var firstName = "Martin"
    var lastName = "Neuhard"
    var theSchool = "Binghamton University"
    if(document.getElementById('fName').value == firstName +=    document.getElementById('lName').value == lastName +=    document.getElementById('schoolName').value == theSchool){
        document.getElementById('content').style.background-color = 'green';
        document.getElementById('content').style.background-image = 'url(http://www.heritage-flag.com/binghamton-university-bearcats/cats.gif)';
        return true;
    }
    else if(changePage() = true){
    
    }
    }
    
    function validName(){ 
    
    var str = document.getElementById('fName').value; 
    var reg = new RegExp("[^a-zA-Z]");
    if (reg.test(str)){ 
        return false;
    }
    else{
        return true;
    }
    }
    Code (markup):
     
    jasonxxx102, Oct 17, 2013 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Your if statements are gibberish and trying too hard. you can't += after a ==

    At least not without a lot more (). Not sure what you're trying to do there but I think you're not automating the checks enough... though the incomplete form with missing attributes and tags certainly can't be helping. NOT that ANY of this is something I'd be doing client-side in the first place. Remember, make the page work WITHOUT scripting before you even THINK about enhancing it further.
     
    deathshadow, Oct 18, 2013 IP
    ryan_uk likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Looking at your code more, do you mean && instead of += ??? Not that there's any reason to = false, which I suspect you meant ==

    Gah, what the devil are you trying to do there?
     
    deathshadow, Oct 18, 2013 IP
  4. jasonxxx102

    jasonxxx102 Well-Known Member

    Messages:
    1,114
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    105
    #4
    It's ok, I ended up getting everything to work. I've never worked with JS before I'm more of a VBA guy so the syntax was all screwed up.
     
    jasonxxx102, Oct 18, 2013 IP
    ryan_uk likes this.
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #5
    That explains a lot. I jump between languages a good deal too -- it's a pain when doing AT&T style and Intel style ASM side-by-side, with src,dest vs. dest,src ... or switching back and forth between php and js where I keep trying to use periods for string addition in js or + for string addition in PHP :D

    Really can drive you nutters after a while -- the minor syntax differences.
     
    deathshadow, Oct 18, 2013 IP
  6. jasonxxx102

    jasonxxx102 Well-Known Member

    Messages:
    1,114
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    105
    #6
    Yea, it's especially tough because I'm totally new to programming
     
    jasonxxx102, Oct 18, 2013 IP