4
4
if (Request .HttpMethod == " HEAD" ) { return ; }
5
5
var vm = new BlogEngine .Core .Data .ViewModels .BlogRollVM ();
6
6
7
- if (Request .Form [" btnAdd" ] != null || Request .Form [" btnUpdate" ] != null )
8
- {
9
- Validation .Add (" txtTitle" , Validator .Required (" Field is required" ));
10
- Validation .Add (" txtDesc" , Validator .Required (" Field is required" ));
11
- Validation .Add (" txtWebsite" , Validator .Required (" Field is required" ), Validator .Url (" Field is URL" ));
12
- Validation .Add (" txtUrl" , Validator .Required (" Field is required" ), Validator .Url (" Field is URL" ));
13
- }
14
7
var id = Request .Form [" hdnId" ];
8
+ var action = Request .Form [" hdnAction" ];
15
9
var br = new BlogRollItem ();
16
10
17
11
if (IsPost )
18
12
{
19
- if (Request . Form [ " btnAdd " ] != null )
13
+ if (action == " add " )
20
14
{
21
- if (Validation .IsValid ())
22
- {
23
- vm .Add (Request .Form );
24
- @:
25
- < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
26
- }
27
- else
28
- {
29
- vm .SaveForm (Request .Form );
30
- }
15
+ vm .Add (Request .Form );
16
+ @: < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
31
17
}
32
- else if (Request . Form [ " btnUpdate " ] != null )
18
+ else if (action == " update " )
33
19
{
34
- if (Validation .IsValid ())
35
- {
36
- vm .Update (Request .Form , id );
37
- @:
38
- < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
39
- }
40
- else
41
- {
42
- vm .SaveForm (Request .Form );
43
- }
20
+ vm .Update (Request .Form , id );
21
+ @: < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
44
22
}
45
- else if (Request . Form [ " btnDelete " ] != null )
23
+ else if (action == " delete " )
46
24
{
47
25
vm .Delete (id );
48
- @:
49
- < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
26
+ @: < script type = " text/javascript" > window .parent .toastr .success (" Completed" );< / script >
50
27
}
51
28
else
52
29
{
69
46
<body class =" widget-edit" >
70
47
<script src =" ~/Scripts/jquery-2.1.4.min.js" ></script >
71
48
<div class =" widget-edit-blogroll" >
72
- <form method =" post" onsubmit = " clearValidation() " id =" frm" name =" frm" >
49
+ <form method =" post" id =" frm" name =" frm" >
73
50
<div class =" form-group" >
74
51
<label >@Resources.labels.title </label >
75
52
<input type =" text" class =" form-control" id =" txtTitle" name =" txtTitle" value =" @br.Title" />
76
- @Html.ValidationMessage( "txtTitle")
53
+ < span id = " lblTitle " name = " lblTitle " class = " field-validation-error " >Field is required</ span >
77
54
</div >
78
55
<div class =" form-group" >
79
56
<label >@Resources.labels.website </label >
80
57
<input type =" text" class =" form-control" id =" txtWebsite" name =" txtWebsite" value =" @br.BlogUrl" />
81
- @Html.ValidationMessage( "txtWebsite")
58
+ < span id = " lblWebsite " name = " lblWebsite " class = " field-validation-error " >Field is required and must be URL</ span >
82
59
</div >
83
60
<div class =" form-group" >
84
61
<label >@Resources.labels.description </label >
85
62
<input type =" text" class =" form-control" id =" txtDesc" name =" txtDesc" value =" @br.Description" />
86
- @Html.ValidationMessage( "txtDesc")
63
+ < span id = " lblDesc " name = " lblDesc " class = " field-validation-error " >Field is required</ span >
87
64
</div >
88
65
<div class =" form-group" >
89
66
<label >@Resources.labels.url </label >
90
67
<input type =" text" class =" form-control" id =" txtUrl" name =" txtUrl" value =" @br.FeedUrl" />
91
- @Html.ValidationMessage( "txtUrl")
68
+ < span id = " lblUrl " name = " lblUrl " class = " field-validation-error " >Field is required and must be URL</ span >
92
69
</div >
93
70
<label class =" control-label" >XFN tag</label >
94
71
<table id =" cblXfn" cellspacing =" 0" cellpadding =" 0" border =" 0" >
125
102
<div class =" form-group" >
126
103
@if (! string .IsNullOrEmpty (Request .Form [" hdnId" ]))
127
104
{
128
- <button type =" submit " id =" btnUpdate" onclick =" submitForm('@id')" name =" btnUpdate" class =" btn btn-success" >
105
+ <button type =" button " id =" btnUpdate" onclick =" submitForm('@id', 'update ')" name =" btnUpdate" class =" btn btn-success" >
129
106
<i class =" fa fa-check" ></i >
130
107
</button >
131
- <button type =" submit" id =" btnDelete" onclick =" submitForm('@id')" name =" btnDelete" class =" btn btn-danger" >
108
+ <button type =" submit" id =" btnDelete" onclick =" submitForm('@id', 'delete' )" name =" btnDelete" class =" btn btn-danger" >
132
109
<i class =" fa fa-trash" ></i >
133
110
</button >
134
111
<button type =" submit" id =" btnCancel" name =" btnCancel" class =" btn btn-default" >
137
114
}
138
115
else
139
116
{
140
- <button type =" submit " id =" btnAdd" name =" btnAdd" class =" btn btn-success" >
117
+ <button type =" button " id =" btnAdd" onclick = " submitForm('','add') " name =" btnAdd" class =" btn btn-success" >
141
118
@Resources.labels.add
142
119
</button >
143
120
}
162
139
{
163
140
<tr >
164
141
<td class =" item-title" >
165
- <a title =" @roll.Title" href =" #" onclick =" submitForm('@roll.Id')" class =" text-ellipsis pull-left" >@roll.Title </a >
142
+ <a title =" @roll.Title" href =" #" onclick =" submitForm('@roll.Id', 'load' )" class =" text-ellipsis pull-left" >@roll.Title </a >
166
143
<a title =" @roll.FeedUrl" class =" external-link pull-right" target =" _blank" href =" @roll.FeedUrl" ><i class =" fa fa-external-link" ></i ></a >
167
144
</td >
168
145
<td >@roll.Description </td >
173
150
</tbody >
174
151
</table >
175
152
<input type =" hidden" id =" hdnId" name =" hdnId" value =" " />
153
+ <input type =" hidden" id =" hdnAction" name =" hdnAction" value =" " />
176
154
</form >
177
155
</div >
178
156
<script >
179
- var submitForm = function (id ) {
157
+ var submitForm = function (id , action ) {
180
158
$ (" #hdnId" ).val (id);
181
- $ (" #frm" ).submit ();
159
+ $ (" #hdnAction" ).val (action);
160
+
161
+ if (action == ' add' || action == ' update' ) {
162
+ clearValidation ();
163
+ if (checkValidation () == true ) {
164
+ $ (" #frm" ).submit ();
165
+ }
166
+ }
167
+ else {
168
+ $ (" #frm" ).submit ();
169
+ }
170
+ }
171
+ var checkValidation = function () {
172
+ if ($ (" #txtTitle" ).val ().length < 1 ) {
173
+ $ (" #lblTitle" ).show ();
174
+ return false ;
175
+ }
176
+ if ($ (" #txtWebsite" ).val ().length < 1 || validateURL ($ (" #txtWebsite" ).val ()) == false ) {
177
+ $ (" #lblWebsite" ).show ();
178
+ return false ;
179
+ }
180
+ if ($ (" #txtDesc" ).val ().length < 1 ) {
181
+ $ (" #lblDesc" ).show ();
182
+ return false ;
183
+ }
184
+ if ($ (" #txtUrl" ).val ().length < 1 || validateURL ($ (" #txtUrl" ).val ()) == false ) {
185
+ $ (" #lblUrl" ).show ();
186
+ return false ;
187
+ }
188
+ return true ;
182
189
}
183
190
var clearValidation = function () {
184
191
$ (' .field-validation-error' ).hide ();
185
192
}
193
+ function validateURL (value ) {
194
+ return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@@)|\/|\?)*)?$/i.test(value);
195
+ }
196
+ clearValidation ();
186
197
</script >
187
198
</body >
188
199
</html >
0 commit comments