Al-geBra
Al-geBra

Reputation: 179

Unable to produce output from span tag

I am working on a simple form from a coursera MOOC on web development. I am using a RegEx in JS to test for valid input, and an onblur effect to display my test' output. For some odd reason it's not working. Here is the code, I'll explain what it does:

<!DOCTYPE HTML>
<html>
<head>
    <title>Week 5 Assignment - Simple Form</title>
    <script type="text/javascript" src="Week5.js"></script>
</head>
<body> <!--The Five Input Elements here are as follows
           1. Text Box
           2. Text Box
           3. Radio Buttons
           4. Date
           5. Button-->
    <form>
        First Name:<br />
        <input type="text" name="firstname" onblur="testFirstName(); "/><span id="firstNamePrompt">&nbsp;</span><br /><br />
        Last Name:<br />
        <input type="text" name="lastname" onblur="testLastName(); "/><span id="lastNamePrompt"></span>&nbsp;<br /><br />
        Gender:<br />
        <input type="radio" name="gender" value="male" />Male<br />
        <input type="radio" name="gender" value="female" />Female<br /><br />
        Date and Year of Birth:<br />
        <input type="date" name="dob" /><br /><br />
        <input type="button" value="Submit" />

    </form>
</body>
</html>

Here, I am creating a simple form and the onblur event is triggered by 2 javascript functions, testFirstName() and testLastName(). I expect to get output on the same line as the textboxes that state that a condition has been or has not been met.

Here is the JavaScript:

function testFirstName() {
    var firstName = document.getElementById('firstname').value;
    var regCheck = /^[a-zA-Z\s\'\-] {2,15} $/;

    if (regCheck.test(firstname)) {
        document.getElementById('firstNamePrompt').style.color = "green";
        document.getElementById('firstnamePrompt').innerHTML = "<strong>Name Accepted!</strong>";
        return true;
    }

    else {
        document.getElementById('firstNamePrompt').style.color = "red";
        document.getElementById('firstNamePrompt').innerHTML = "<strong>Name must be between 2-15 characters!</strong>";
        return false;
    }
}

function testLastName() {
    var firstName = document.getElementById('lastname').value;
    var regCheck = /^[a-zA-Z\s\'\-] {2,25} $/;

    if (regCheck.test(firstname)) {
        document.getElementById('lastNamePrompt').style.color = "green";
        document.getElementById('lastnamePrompt').innerHTML = "<strong>Name Accepted!</strong>";
        return true;
    }

    else {
        document.getElementById('lastNamePrompt').style.color = "red";
        document.getElementById('lastNamePrompt').innerHTML = "<strong>Name must be between 2-25 characters!</strong>";
        return false;
    }
}

I have a reg. expression that tests that characters can only be A-Z (upper and lowercase), Have a space via the 's', or have an apostrophe or a dash in the name. Those are the only allowed characters based on the regex. I am not sure why I don't see text "Name Accepted!" or "Name must be between x & y characters". Please let me know if there is anything else I need to explain. I hope this is not verbose.

Upvotes: 1

Views: 377

Answers (2)

Ivan86
Ivan86

Reputation: 5708

An element needs to have and id in order to be able to target it from javascript with document.getElementById().

After adding an id to firstname and lastname everything works fine.

Also your span ids have upper case letters, which you didn't mention in your javascript.

Here is the corrected code: (run the snippet)

function testFirstName() {
    var firstName = document.getElementById('firstname').value;
    var regCheck = /^[a-zA-Z\s\'\-]{2,25}$/;

    if (regCheck.test(firstName)) {
        document.getElementById('firstNamePrompt').style.color = "green";
        document.getElementById('firstNamePrompt').innerHTML = "<strong>Name Accepted!</strong>";
        return true;
    }

    else {
        document.getElementById('firstNamePrompt').style.color = "red";
        document.getElementById('firstNamePrompt').innerHTML = "<strong>Name must be between 2-15 characters!</strong>";
        return false;
    }
}

function testLastName() {
    var lastName = document.getElementById('lastname').value;
    var regCheck = /^[a-zA-Z\s\'\-]{2,25}$/;

    if (regCheck.test(lastName)) {
        document.getElementById('lastNamePrompt').style.color = "green";
        document.getElementById('lastNamePrompt').innerHTML = "<strong>Name Accepted!</strong>";
        return true;
    }

    else {
        document.getElementById('lastNamePrompt').style.color = "red";
        document.getElementById('lastNamePrompt').innerHTML = "<strong>Name must be between 2-25 characters!</strong>";
        return false;
    }
}
<!DOCTYPE HTML>
<html>
<head>
    <title>Week 5 Assignment - Simple Form</title>
    <script type="text/javascript" src="Week5.js"></script>
</head>
<body> <!--The Five Input Elements here are as follows
           1. Text Box
           2. Text Box
           3. Radio Buttons
           4. Date
           5. Button-->
    <form>
        First Name:<br />
        <input type="text" id="firstname" name="firstname" onblur="testFirstName(); "/><span id="firstNamePrompt">&nbsp;</span><br /><br />
        Last Name:<br />
        <input type="text" id="lastname" name="lastname" onblur="testLastName(); "/><span id="lastNamePrompt"></span>&nbsp;<br /><br />
        Gender:<br />
        <input type="radio" name="gender" value="male" />Male<br />
        <input type="radio" name="gender" value="female" />Female<br /><br />
        Date and Year of Birth:<br />
        <input type="date" name="dob" /><br /><br />
        <input type="button" value="Submit" />

    </form>
</body>
</html>

Upvotes: 3

Sergey Benzenko
Sergey Benzenko

Reputation: 280

In order to use getElementById method, you should provide the id attribute to your inputs:

<input type="text" id="firstname" name="firstname" onblur="testFirstName(); "/>

And

<input type="text" id="lastname" name="lastname" onblur="testLastName(); "/>

Upvotes: 2

Related Questions