forked from ArchanaAvv/June2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidationAssignment.js
130 lines (115 loc) · 3.63 KB
/
validationAssignment.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Created by salma on 6/14/2015.
*/
$(function(){
addFormValidationRules();
$('#btnSubmit').click(function(event){
event.preventDefault();
event.stopPropagation();
//1.Read the data entered by user in to an object
var userDetails = getUserEnteredDetails();
//2. validate the data
var isFormValid = checkFormValidation();
//3. save or post
if(isFormValid) {
saveUserDetails(userDetails);
}else{
alert('form is not valid');
}
});
function addFormValidationRules(){
var formValidationConfig = {
rules: {
firstName: {
required: true,
minLength: 4
},
lastName: {
required: true,
minLength: 4
},
emailId: {
required: true
//email: true
},
password: {
required: true
},
mobile: {
required: true
//digits: true
},
birthday: {
required: true
},
gender: {
required: true
}
},
messages: {
firstName: {
required: 'Name is a Required Field, Please enter first name',
minLength: 'Please enter minimum of 4 characters'
},
lastName: {
required: 'Name is a Required Field, Please enter last name',
minLength: 'Please enter minimum of 4 characters'
},
emailId: {
required: "Email is a Required Field"
//email: "Please enter a valid email"
},
password:{
required:"Please enter your password"
},
mobile: {
required: 'Mobile is a Required Field, Please enter mobile number'
//digits:'enter digits only'
},
birthday:{
required:"Date of birth is a Required Field"
},
gender:{
required:"gender is a Required Field"
}
}
};
$('#userDetailsForm').validate(formValidationConfig);
}
function checkFormValidation(){
return $('#userDetailsForm').valid();
}
function getUserEnteredDetails(){
var userDetails = {
firstName : $('#firstName').val(),
lastName : $('#lastName').val(),
emailAddress : $('#emailInput').val(),
password: $('#password').val(),
mobileNumber: $('#mobileNumber').val(),
birthday: $('#birthday').val(),
gender: $('#gender').val()
};
return userDetails;
}
function saveUserDetails(userDetails){
ajaxPost(userDetails,"http://localhost:9400/createAccount");
}
function ajaxPost(formData,formUrl){
var ajaxConfig = {
url: formUrl,
method:"POST",
timeout: "10000",//10secs
data : formData,
dataType : "json",
success : function(data){
alert('Data Saved Successfully!');
},
error : function(){
alert('error while posting');
},
complete : function(){
}
};
$.ajax(ajaxConfig);
}
});