L05 Javascript2
L05 Javascript2
More on JavaScript
Joseph Tonien
Form validation
<form action="myService" method="get"
onSubmit="return validateForm()">
Use form attribute
... your form goes here ... onSubmit to check input
before form submission
</form>
<script>
function validateForm() {
if (... something wrong ...) {
return false;
}
return true;
} When function returns
</script> false, form will not be
submitted
Form validation
Example 1: we want user to fill out the email, if email is not filled out then we will
alert the user
</form>
www.uow.edu.au/~dong/w3/example/js/validation1.html
Form validation
Example 1: we want user to fill out the email, if email is not filled out then we will
alert the user
<script>
function validateForm() {
var email = document.getElementById("email").value;
return true;
}
</script>
Example 2:
http://www.uow.edu.au/~dong/w3/example/js/validation2.html
return true;
}
</script>
If user didn’t fill out the email, we want to display an error message.
We use a span element as a placeholder for the error message.
</form>
Form validation
If user didn’t fill out the email, we want to display an error message.
We use a span element as a placeholder for the error message.
function validateForm() {
var email = document.getElementById("email").value;
return false;
}
return true;
}
<span id="emailError"></span>
Form validation
We want the error message has the color red.
<style>
#emailError{
color: red;
}
</style>
<span id="emailError"></span>
Form validation
Example 4: We want to have two input fields. One for email and another one for
email confirmation. User has to fill in the same email for both input fields.
</form>
http://www.uow.edu.au/~dong/w3/example/js/validation4.html
Form validation
function validateForm() {
var email = document.getElementById("email").value;
if (email == null || email.trim() == ""){
document.getElementById("emailError").innerHTML
= "Email must be filled out";
return false;
}
if(email.trim() != email2.trim()){
document.getElementById("emailError2").innerHTML =
"Email does not matched";
return false;
}
return true;
}
Form validation
We want all the error messages have the color red.
<style>
#emailError{
color: red;
}
#emailError2{
color: red;
}
</style>
<span id="emailError"></span>
<span id="emailError2"></span>
Form validation
Better solution: using class
<style>
.errorMessage{
color: red;
}
</style>
Suppose that user fixed the error by filling out the first email, but leaving the second
email field blank.
When the user clicks Submit, we will see that the error message next to the first
input field still shows.
http://www.uow.edu.au/~dong/w3/example/js/validation4.html
http://www.uow.edu.au/~dong/w3/example/js/validation4b.html
function validateForm() {
var email = document.getElementById("email").value;
function validateForm() {
...
var email2 = document.getElementById("email2").value;
function validateForm() {
...
if(email.trim() != email2.trim()){
document.getElementById("emailError2").innerHTML =
"Email does not matched";
return false;
}else{
document.getElementById("emailError2").innerHTML = "";
}
...
Form validation
Final touch: we want to remove all whitespaces in the two input fields before submit
function validateForm() {
var email = document.getElementById("email").value;
function validateForm() {
...
var email2 = document.getElementById("email2").value;
http://www.uow.edu.au/~dong/w3/example/js/validation5.html
Ask user a simple math problem, only submit the form if user answers correctly
function validateForm() {
...
var answer = prompt("What is 1+2 ?");
if(answer == null || answer != 3){
return false;
}
...
Form validation
Example 5:
http://www.uow.edu.au/~dong/w3/example/js/validation5.html
Ask user a simple math problem, only submit the form if user answers correctly
Math.floor(x):
returns the greatest integer below x
for example, Math.floor(4.6) = 4
.753 * 10 = 7.53
Math.floor(7.53) = 7
http://www.uow.edu.au/~dong/w3/example/js/validation5b.html
Generate random question
function validateForm() {
...
var x = Math.floor(Math.random() * 10) + 1;
var y = Math.floor(Math.random() * 10) + 1;
var correctAnswer = x + y;
<html>
<head></head>
<body>
<form action="myService" method="get">
Email:
<input type="text“ id="email" name="email"
title="Email should not be empty"
placeholder = "error if you see me!"
oninvalid = "this.setCustomValidity( 'hello already tell u see me is error, still submit!');"
required>
<input type="submit" onsubmit="return validateForm()">
</form>
</body>
</html>
Regular expression
A regular expression describes a pattern of characters
/pattern/
[abc] Only a, b, or c
[^abc] Not a, b, nor c
[a-z] Characters a to z
[0-9] Numbers 0 to 9
\d Any Digit
\D Any Non-digit character
\s Any Whitespace
\S Any Non-whitespace character
Regular expression
A regular expression describes a pattern of characters
/pattern/
. Any Character
\. Period
{m} m Repetitions
{m,n} m to n Repetitions
. Any Character
\. Period
. \ / + * ? ^ [ ] $ ( ) { } |
Regular expression
Example 1:
<form action="myService" method="get">
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex1.html
Regular expression
We can use javascript to check regular expression
<script>
function validateForm(){
var input = document.getElementById("t1").value;
if(/^[A-Z][a-z], [A-Z][a-z]$/.test(input) == false){
alert("input does not match the pattern");
return false;
}
return true;
}
</script>
<script>
function validateForm(){
var input = document.getElementById("t1").value;
if(/^[A-Z][a-z], [A-Z][a-z]$/.test(input) == false){
alert("input does not match the pattern");
return false;
}
return true;
}
</script>
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex2.html
Regular expression
Example 3:
<form action="myService" method="get">
</form>
http://www.uow.edu.au/~dong/w3/example/js/regex3.html
Regular expression
Example 4:
pattern="^[0-9]{2,}$"
pattern="^\d{2,}$"
Regular expression
Example 5:
pattern="^.{5,}$"
5 or more characters
Example 6:
pattern="^.{4,10}$"
4 to 10 characters
Example 7:
pattern="^[0-9]{1,3}\.[0-9]{1,3}$"
pattern="^[^0-9][0-9]$"
pattern="^\D\d$"
Example 9:
pattern="^[0-9]+[a-z]*$"
Example 10:
pattern="^http(s?):\/\/.+$"
pattern="^[0-9]{4}-[0-9]{4}$"
Example 12:
pattern="^[0-9]{2}:[0-9]{2}:[0-9]{4}$"
Example 13:
pattern="^(NSW|ACT|NT|QLD|SA|TAS|VIC|WA) [0-9]{4}$"
pattern="^(0[1-9]|1[0-2])-20(1[0-9]|2[0-5])$"
http://www.w3schools.com/js
http://www.w3schools.com/jsref/jsref_obj_regexp.asp