COS30043 Lecture 06 - Forms and Validation
COS30043 Lecture 06 - Forms and Validation
Contents
Form Validation:
– Using HTML 5 Form Attributes
– Using VueJS
– Using Vuetify
1
HTML 5 Form Validation Attributes
•required: Specifies whether a form field needs to be filled
in before the form can be submitted.
•minlength and maxlength: Specifies the minimum and
maximum length of textual data (strings)
•min and max: Specifies the minimum and maximum
values of numerical input types
•type: Specifies whether the data needs to be a number,
an email address, or some other specific preset type.
<input type=“date” …
<input type=“time”…
<input type=“email” …
•pattern: Specifies a regular expression that defines a
pattern the entered data needs to follow.
3 - Interface Design and Development, © Swinburne
<form method="post"
action="https://mercury.swin.edu.au/it000000/formtest.php" >
2
Contents
Form Validation:
– Using HTML 5 Form Attributes
– Using VueJS
– Using Vuetify
3
VueJS Form Validation – HTML
<div id="app">
<form @submit="checkForm“ method="post“
action="https://mercury.swin.edu.au/it000000/formtest.php" novalidate >
<div v-if="errors.length">
<p>Please correct the following error(s):</p>
Error message
<ul> <li v-for="error in errors">{{ error }}</li> </ul>
</div>
<p>
<label for="fName">First Name</label> Text input
<input type="text" name="fName" id="fName" v-model="fName"/>
</p>
<p><input type="submit" value="submit"> </p> Submit button
</form>
</div>
7 - Interface Design and Development, © Swinburne
methods: {
checkForm: function(e) {
this.errors = [ ]; var result = true;
if ( !this.fName) {
this.errors.push("First name required")
result = false;
}
if (!result)
e.preventDefault(); //prevent form submission
}
}
})
app.mount('#app')
8 - Interface Design and Development, © Swinburne
4
Contents
Form Validation:
– Using HTML 5 Form Attributes
– Using VueJS
– Using Vuetify
Vuetify
• Complete UI • Material design (https://material.io/)
was created by Google to help teams
framework built on build high-quality digital experiences for
Android, iOS, Flutter, and the web.
top of Vue.js • Material Design is inspired by the
physical world and its textures,
• Vuetify is a Vue UI including how they reflect light and cast
shadows. Material surfaces reimagine
Library with Material the mediums of paper and ink.
design components • There are different components for
creating user interface, such as cards,
floating buttons, app bar, text fields…
10
5
Vuetify
• https://vuetifyjs.com/
11
Vuetify - Installation
12
6
Template for VueJS with Vuetify
<!DOCTYPE html>
<html>
<head>
<!-- link to material design -->
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css"
rel="stylesheet">
<!-- link to vuetify css -->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css"
rel="stylesheet">
13
<script>
const { createApp } = Vue
const { createVuetify } = Vuetify
const app = createApp()
const vuetify = createVuetify()
app.use(vuetify).mount('#app')
</script>
</body>
</html>
14 - Interface Design and Development, © Swinburne
14
7
Vuetify Example
<v-container fluid>
<v-btn color="primary" >
<v-icon>mdi-pencil</v-icon> <!-- mdi: material design icon -->
</v-btn>
<v-container fluid>
<v-alert type="success">
I'm a success alert.
</v-alert>
<v-alert type="info">
I'm an info alert.
</v-alert>
</v-container>
15
16
8
Using Vuetify for form validation
• Vuetify has a component v-form which makes
it easy to add validation to form inputs.
• All the input components has a rules prop
which accepts a mixed array of types function,
boolean and string .
17
</v-form>
18
9
Vuetify From Validation Example (JS)
const { createApp } = Vue
const { createVuetify } = Vuetify
const vuetify = createVuetify( )
const app = createApp({
data: () => ({
firstName: '', email: '',
nameRules: [
v => !!v || 'Name required',
v => (v && v.length <= 10) || 'Name must be less than 10
characters’
],
emailRules: [
v => !!v || 'E-mail is required',
v => /[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$/.test(v) || 'E-
mail must be valid’,
]
})
})
19 - Interface Design and Development, © Swinburne
app.use(vuetify).mount('#app')
19
WHAT’S NEXT?
- API 1
20
10