diego707
Anime Hype Generator
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial we will create a HTML Form Validation using jQuery. This code will automatically validate the form inputs when the user enter some text. The code use jQuery focusout function to detect if the user enter a text and then validate it with a conditional statement if. This is a user-friendly program feel free to modify it.
We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.
Getting Started:
This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
And this is the link for the jquery that i used in this tutorial https://jquery.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
Creating the Script
This code contains the script of the application. This code will automatically validate the HTML form when text is entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
There you have it we successfully created a HTML Form Validation using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.
Getting Started:
This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.
And this is the link for the jquery that i used in this tutorial https://jquery.com/.
The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
- <!DOCTYPE html>
- <html
lang
=
"en"
>
- <head
>
- <link
rel
=
"stylesheet"
type
=
"text/css"
href
=
"css/bootstrap.css"
/
>
- <meta
charset
=
"UTF-8"
name
=
"viewport"
content
=
"width=device-width, initial-scale=1"
/
>
- </
head
>
- <body
>
- <nav class
=
"navbar navbar-default"
>
- <div
class
=
"container-fluid"
>
- <a
class
=
"navbar-brand"
href
=
"https://sourcecodester.com"
>
Sourcecodester</
a
>
- </
div
>
- </
nav>
- <div
class
=
"col-md-3"
></
div
>
- <div
class
=
"col-md-6 well"
>
- <h3
class
=
"text-primary"
>
HTML Form Validation Using jQuery Source Code</
h3
>
- <hr
style
=
"border-top:1px dotted #ccc;"
/
>
- <div
class
=
"col-md-5"
>
- <form
method
=
"POST"
id
=
"myform"
action
=
"index.html"
>
- <div
class
=
"form-group"
>
- <h4
>
Name</
h4
>
- <input
type
=
"text"
name
=
"name"
id
=
"name"
class
=
"form-control"
>
- <span
id
=
"name_error"
class
=
"text-danger"
></
span
>
- </
div
>
- <div
class
=
"form-group"
>
- <h4
>
Username</
h4
>
- <input
type
=
"text"
name
=
"username"
id
=
"username"
class
=
"form-control"
/
>
- <span
id
=
"username_error"
class
=
"text-danger"
></
span
>
- </
div
>
- <div
class
=
"form-group"
>
- <h4
>
Password</
h4
>
- <input
type
=
"password"
name
=
"password"
id
=
"password"
class
=
"form-control"
/
>
- <span
id
=
"password_error"
class
=
"text-danger"
></
span
>
- </
div
>
- <br
/
>
- <center
><button
class
=
"btn btn-primary"
style
=
"width:80%;"
>
Submit</
button
></
center
>
- </
form
>
- </
div
>
- </
div
>
- </
div
>
- </
body
>
- <script
src
=
"js/jquery-3.2.1.min.js"
></
script
>
- <script
src
=
"js/script.js"
></
script
>
- </
html
>
Creating the Script
This code contains the script of the application. This code will automatically validate the HTML form when text is entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
- $(
document)
.ready
(
function
(
)
{
- $(
"#name_error"
)
.hide
(
)
;
- $(
"#username_error"
)
.hide
(
)
;
- $(
"#password_error"
)
.hide
(
)
;
- $(
"#name"
)
.focusout
(
function
(
)
{
- var
name =
$(
"#name"
)
.val
(
)
.length
;
- if
(
name <
8
||
name >
24
)
{
- $(
"#name_error"
)
.html
(
"Name must be 5 long"
)
;
- $(
"#name_error"
)
.show
(
)
;
- }
else
{
- $(
"#name_error"
)
.hide
(
)
;
- }
- }
)
- $(
"#username"
)
.focusout
(
function
(
)
{
- var
username =
$(
"#username"
)
.val
(
)
.length
;
- if
(
username <
8
||
username >
24
)
{
- $(
"#username_error"
)
.html
(
"Username must be 8 - 24 characters"
)
;
- $(
"#username_error"
)
.show
(
)
;
- }
else
{
- $(
"#username_error"
)
.hide
(
)
;
- }
- }
)
- $(
"#password"
)
.focusout
(
function
(
)
{
- var
password =
$(
"#password"
)
.val
(
)
.length
;
- if
(
password <
5
)
{
- $(
"#password_error"
)
.html
(
"Password too short"
)
;
- $(
"#password_error"
)
.show
(
)
;
- $(
"#password_error"
)
.attr
(
'class'
,
"text-warning"
)
;
- }
else
if
(
password >
12
)
{
- $(
"#password_error"
)
.html
(
"Password too long"
)
;
- $(
"#password_error"
)
.show
(
)
;
- $(
"#password_error"
)
.attr
(
'class'
,
"text-danger"
)
;
- }
else
{
- $(
"#password_error"
)
.hide
(
)
;
- }
- }
)
- }
)
;
There you have it we successfully created a HTML Form Validation using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Download
You must upgrade your account or reply in the thread to view hidden text.