Fill This Out

by Nick Jensen on November 25, 2013

As all of you know, I’ve been reading John Pollock’s JavaScript: A Beginner’s Guide. I’m only two chapters away from the end, and I cannot lie, I’ll be glad to have it out of my hair. Learning  JavaScript is turning out to be  significantly more difficult than I anticipated. As I read deeper into the book, the concepts become harder to learn. On many occasions, I read the beginning of the chapter and think to myself, “Oh, this doesn’t look to hard.” After that, I turn the page and run into a brick wall. I’ve learned to grow suspicious of any JavaScript concept that looks “easy”, because nine out of ten times, I end up dead wrong, and learning how to manage forms with JavaScript is no exception.

To be fair, not all of the concepts are difficult to learn. For instance, I can easily figure out and remember how to count all the elements within a form.

<form>

<label>Name</label><input type = “text”>

<label>E-mail:</label><input type = “text”>

<input type=”submit” value=”submit”>

</form>

document.write(“There are ” + document.forms(0).length + ” elements in this form.”);

In the above code, I created a form, told the browser to count the number of elements in the form and then write that number in a sentence: “There are 3 elements in this form.” You must always start with zero when creating any type of array (which I covered in a previous blog). form 1 becomes  document.forms(0), form 2 becomes document.forms(1), and so on. Pretty simple, right? Well, let’s try something a bit more challenging (and when I say a bit, I mean a lot). Let’s make a script to validate user input within a form.

<form id=”confirm_password”>

<label for=”confirm_password”>Please Enter the Password:</label>

<input type=”text” id=”confirm_password”><br>

<input type=”submit” value=”submit”>

</form>

<script type=”text/javascript”>

var confirm_password = document.GetElementById(“confirm_password “);

confirm_password.addEventListener(“submit”, function(event) {

var confirm_password = document.GetElementById(“phone”).value,

tomatch = /lastc/;

event.preventDefault();

if (tomatch.test(lastc)) {

alert(“Password Confirmed”);

}

else {

alert(“You forgot the password again. You should really write it down.”);

}

}, false);

</script>

I know, the process is mind-numbing. Luckily, I’m at the end of the book. While I may not have mastered JavaScript, I have learned enough to recognize the basic code layouts. Of course, I plan visit JavaScript again in the future. You can’t become an expert at anything by simply going over the material once.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: