CodeIn House

  • Laravel
  • WordPress
  • jQuery
  • Javascript
  • Contact
Home   Javascript   How to Perform Dynamic Form Validation using jQuery and Java ...

How to Perform Dynamic Form Validation using jQuery and Javascript ?

August 15, 2017 by SNK

dynamic form validation using jquery

In this tutorial we will do dynamic form validation using jquery and javascript, by the help of this you no longer have to write code for each of the fields while submitting the form.

This type of validation is generally performed when you are trying to go to different page after entering some data into the form and system will prompt saying “Some data may not be submitted do you still want to proceed?” type of messages.

So today we are going to do same sort of validation using jquery and javascript.

Lets get started :-

Prepare a demo html page with a form.

HTML Template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<div class="contaienr">
<div class="row" style="margin-top: 30px;">
<div class="col-md-5 col-sm-offset-3">
<form action="" onsubmit="return checkData()" name="myform">
<div class="form-group">
<input type="text" id="" name="name" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name1" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name2" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name3" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name4" class="form-control" />
</div>
<div class="form-group">
<button class="btn btn-primary btn-sm">Send Button</button>
</div>
</form>
</div>
</div>
</div>

Now some javascript code,

script
JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function checkData() {
 
event.preventDefault();
var fields = ["name", "name1", "name2", "name3", "name4"];
 
var i, l = fields.length;
var fieldname;
var not_empty_check = '';
for (i = 0; i < l; i++) {
fieldname = fields[i];
if (document.forms["myform"][fieldname].value != "") {
not_empty_check = 1;
}
}
 
if(not_empty_check <= 1) {
alert("fields are missing");
}
return false;
}

 

If you are still confused, why dont you have full code to more clear on what we have done here,

Full Page Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dynamic Javascript and jQuery Validaton Demo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
</head>
<body>
 
<div class="contaienr">
<div class="row" style="margin-top: 30px;">
<div class="col-md-5 col-sm-offset-3">
<form action="" onsubmit="return checkData()" name="myform">
<div class="form-group">
<input type="text" id="" name="name" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name1" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name2" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name3" class="form-control" />
</div>
<div class="form-group">
<input type="text" id="" name="name4" class="form-control" />
</div>
<div class="form-group">
<button class="btn btn-primary btn-sm">Send Button</button>
</div>
</form>
</div>
</div>
</div>
 
 
 
<!-- jQuery -->
<script src="https://code.jquery.com/jquery.js"></script>
<!-- Bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
 
<script>
function checkData() {
 
event.preventDefault();
var fields = ["name", "name1", "name2", "name3", "name4"];
 
var i, l = fields.length;
var fieldname;
var not_empty_check = '';
for (i = 0; i < l; i++) {
fieldname = fields[i];
if (document.forms["myform"][fieldname].value != "") {
not_empty_check = 1;
}
}
 
if(not_empty_check <= 1) {
alert("fields are missing");
}
return false;
}
</script>
</body>
</html>

 

SHARE ON
Buffer

Enjoyed this article?

Like us on

Javascript dynamic form validation using javascript dynamic form validation using jquery jquery validate add rules dynamically

Avatar for SNK

About SNK

Hello Welcome to my Blog. I develop Websites Using Laravel Framwork & WordPress. Get Latest updates on Facebook | Twitter

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get Connected !! With Us

TOP POSTS

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

TUTORIALS TILL DATE

  • September 2022 (6)
  • June 2021 (7)
  • October 2020 (5)
  • September 2020 (6)
  • September 2018 (14)
  • August 2018 (3)
  • July 2018 (4)
  • March 2018 (8)
  • February 2018 (5)
  • January 2018 (1)
  • December 2017 (2)
  • August 2017 (8)
  • May 2017 (1)
  • April 2017 (1)
  • March 2017 (4)
  • February 2017 (3)
  • January 2017 (4)

CATEGORIES

  • Angular (2)
  • CSS3 (3)
  • D3 (3)
  • HTML5 (7)
  • JAVA (11)
  • Javascript (20)
  • jQuery (8)
  • Laravel (35)
  • Others (3)
  • PHP (11)
  • Spring (2)
  • WordPress (10)

Top Categories

  • Angular
  • CSS3
  • D3
  • HTML5
  • JAVA
  • Javascript
  • jQuery
  • Laravel
  • Others
  • PHP
  • Spring
  • WordPress

Get in Touch

DMCA.com Protection Status

Recent Articles

  • How to Setup Spring MVC Project From Scratch in Intellij IDEA ?
  • Spring Configuration Class in JAVA [Class VS XML Config]
  • Annotations Based Configuration in Spring Framework
  • How to Configure Spring Framework with XML Configurations?
  • How to Remove Project Name from URL in JSP Project in Intellij IDEA ?

© 2012-22 CodeIn House.  •  All Rights Reserved.