44 if (Request .HttpMethod == " HEAD" ) { return ; }
55 var vm = new BlogEngine .Core .Data .ViewModels .BlogRollVM ();
66
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- }
147 var id = Request .Form [" hdnId" ];
8+ var action = Request .Form [" hdnAction" ];
159 var br = new BlogRollItem ();
1610
1711 if (IsPost )
1812 {
19- if (Request . Form [ " btnAdd " ] != null )
13+ if (action == " add " )
2014 {
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 >
3117 }
32- else if (Request . Form [ " btnUpdate " ] != null )
18+ else if (action == " update " )
3319 {
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 >
4422 }
45- else if (Request . Form [ " btnDelete " ] != null )
23+ else if (action == " delete " )
4624 {
4725 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 >
5027 }
5128 else
5229 {
6946<body class =" widget-edit" >
7047 <script src =" ~/Scripts/jquery-2.1.4.min.js" ></script >
7148 <div class =" widget-edit-blogroll" >
72- <form method =" post" onsubmit = " clearValidation() " id =" frm" name =" frm" >
49+ <form method =" post" id =" frm" name =" frm" >
7350 <div class =" form-group" >
7451 <label >@Resources.labels.title </label >
7552 <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 >
7754 </div >
7855 <div class =" form-group" >
7956 <label >@Resources.labels.website </label >
8057 <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 >
8259 </div >
8360 <div class =" form-group" >
8461 <label >@Resources.labels.description </label >
8562 <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 >
8764 </div >
8865 <div class =" form-group" >
8966 <label >@Resources.labels.url </label >
9067 <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 >
9269 </div >
9370 <label class =" control-label" >XFN tag</label >
9471 <table id =" cblXfn" cellspacing =" 0" cellpadding =" 0" border =" 0" >
125102 <div class =" form-group" >
126103 @if (! string .IsNullOrEmpty (Request .Form [" hdnId" ]))
127104 {
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" >
129106 <i class =" fa fa-check" ></i >
130107 </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" >
132109 <i class =" fa fa-trash" ></i >
133110 </button >
134111 <button type =" submit" id =" btnCancel" name =" btnCancel" class =" btn btn-default" >
137114 }
138115 else
139116 {
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" >
141118 @Resources.labels.add
142119 </button >
143120 }
162139 {
163140 <tr >
164141 <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 >
166143 <a title =" @roll.FeedUrl" class =" external-link pull-right" target =" _blank" href =" @roll.FeedUrl" ><i class =" fa fa-external-link" ></i ></a >
167144 </td >
168145 <td >@roll.Description </td >
173150 </tbody >
174151 </table >
175152 <input type =" hidden" id =" hdnId" name =" hdnId" value =" " />
153+ <input type =" hidden" id =" hdnAction" name =" hdnAction" value =" " />
176154 </form >
177155 </div >
178156 <script >
179- var submitForm = function (id ) {
157+ var submitForm = function (id , action ) {
180158 $ (" #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 ;
182189 }
183190 var clearValidation = function () {
184191 $ (' .field-validation-error' ).hide ();
185192 }
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 ();
186197 </script >
187198</body >
188199</html >
0 commit comments