Below are the basic <FORM> with validation before the form submitted.
1 <script>
2 function validateform() {
3 $.ajax({
4 type: "POST",
5 url: "/country/checkcountry",
6 data: "code="+countrycode+"&name="+countryname,
7 dataType: 'json',
8 success: function (data, status) {
9 if (data.result != 'OK') {
10 console.log('check ok');
11 return false
12 } else {
13 console.log('check not ok');
14 return true
15 }
16 },
17 error: function (data, status) {
18 console.log('check error')
19 return false
20 }
21 });
22 console.log('should never show');
23
24 return false; //For testing purpose, it should be return true
25 }
26 </script>
27
28 <FORM action="/country/addcountry" method="post" onsubmit="return validateform()">
29 ..........................
30 </FORM>
At the first glance, the java script seem logically fine. Actually, there are TWO major issue in that java script!