Hey, this is Ralph and in this video, I want to use some JavaScript to check a basic form for empty text strings. It's going to be a really simple one. I have a web page started, and at the bottom of the page, there's a heading with headline one. I'm going to create a quick form with two text inputs. Here's the code for the form:
To make it more official, I'll add fieldset tags: Let's see how it looks on the browser. After refreshing the page, the form is displayed with the text inputs. When I submit, it's hard to notice, but if you look at the address bar, there's a little pound sign indicating the form has been submitted. Now, I want to add an alert or warning to appear if the user tries to submit the form with empty fields. I'll add a script section in the head of my page. I've created a function called "checkForBlank" without any arguments. Inside the function, I've written an if statement that checks if the value of the element with ID "Fname" is empty. If it is, an alert pops up with the message "Please enter your first name". I also added a "return false" statement so that the form doesn't submit. To trigger this function when someone submits the form, I'll add an event handler called "onsubmit" to the form tag: