-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_me.js
More file actions
91 lines (86 loc) · 3.32 KB
/
validate_me.js
File metadata and controls
91 lines (86 loc) · 3.32 KB
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
jQuery(document).ready(function (){
jQuery.validator.addMethod('validName', function (value, element) {
if (/^[a-zšđčćžA-ZŠĐČĆŽ ]+$/gi.test(value)) {
return true;
} else {
return false;
};
});
jQuery.validator.addMethod('validEmail', function (value, element) {
if (/^([a-zA-Z0-9_\-\.]+)\+?([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/.test(value)) {
return true;
} else {
return false;
};
});
jQuery.validator.addMethod('validMessage', function (value, element) {
if (/^[a-zšđčćžA-ZŠĐČĆŽ 0-9 .,!?:;-]+$/gi.test(value)) {
return true;
} else {
return false;
};
});
jQuery("#contact_me").submit(function(event){
event.preventDefault();
}).validate({
rules: {
name: {
required: true,
validName: true,
minlength: 6
},
email: {
required: true,
// Specify that email should be validated
// by the built-in "email" rule
email: true,
validEmail: true
},
message:{
required: true,
validMessage: true,
maxlength: 500
}
},
messages: {
name: {
required: 'First and last name cannot be empty!',
validName: 'The first and last names can contain only letters and spaces!"',
minlength: 'First and last name must have a minimum of 6 letters'
},
email:{
required: 'The e-mail can\'t be empty!',
validEmail: 'The e-mail address is incorrect!',
email: 'Enter <em>valid</em> e-mail!'
},
message: {
required: 'The contents of the message cannot be empty!',
validMessage: 'he content of message cannot be special signs!',
maxlength: 'The message can contain a maximum of 500 characters!'
}
},
submitHandler: function (form) {
//Your code for AJAX starts
var values = jQuery('#contact_me').serialize();
jQuery.ajax({
url: 'form_process.php',
type: "post",
data: values,
dataType: 'json',
cache: false,
success: function (data) {
//alert("success");
jQuery("#response").text(data.content);
},
error: function (data) {
//alert("error");
jQuery("#response").text("An error occurred");
}
//Your code for AJAX Ends
});
// Clear all data after submit
var form = document.getElementById('contact_me').reset();
return false;
}
});
});