javascript - i have some validation on an email input with an ajax call the validation runs but not the call can any give some help as to why this is happening? -


this email input:

<div class="input-group emailinput" style="min-width:300px;max-width:637px;">     <input type="email" class="form-control input1" id="emailaddress" placeholder="email address">     <span class="input-group-btn">     <button class="btn btn-secondary input2" type="button" onclick="validate()" style="padding:0px;border:0px;"><div style="padding:7px; background-color:#ff2b68;color:white;">></div></button>     </span> </div> 

the script:

function validateemail(email) {     var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za-z\-0-9]+\.)+[a-za-z]{2,}))$/;         return re.test(email);     }      function validate()      {         $("#result").text("");          var email = $("#emailaddress").val();          if (validateemail(email)) {             $("#result").text(" email sent.");             $("#result").css("color", "green");             function validate()             {                 var settings = {                   "async": true,                   "crossdomain": true,                   "url": "https://mandrillapp.com/api/1.0/messages/send-template.json",                   "method": "post",                   "headers": {                     "content-type": "application/json"                   },                   "processdata": false,                   "data": "{\n  \"key\": \"key goes here\",\n\t\"message\":{\n\t\t\"subject\": \"test invitation\",\n\t\t\"from_email\": \"info@test.co.za\",\n\t\t\"from_name\": \"test group\",\n\t\t\"to\": [{\"email\": \"email.com\", \"name\": \"applicant\"}]\n\t},\n\t\"template_content\": \n\t[\n\t\t{\n\t\t\t\"name\": \"test\",\n\t\t\t\"content\": \"https://www.test.co.za/invite/\"\n\t\t},\n\t\t{\n\t\t\t\"name\": \"createaccount\",\n\t\t\t\"content\": \"test\" \n\t\t}\n\t],\n  \"template_name\": \"test-template\"\n}"                 }                 $.ajax(settings).done(function (response) {                   console.log(response);                 });             };          }else {             $("#result").text(" please enter valid email address.");             $("#result").css("color", "red");         }         return false;     }      $("form").bind("submit", validate); 

you have wrong validate function declaration inside validate function. not need ajax call wrapped inside this. below code work fine.

var validate = function()      {         $("#result").text("");          var email = $("#emailaddress").val();          if (validateemail(email) == true) {             $("#result").text(" email sent.");             $("#result").css("color", "green");                   var settings = {                   "async": true,                   "crossdomain": true,                   "url": "https://mandrillapp.com/api/1.0/messages/send-template.json",                   "method": "post",                   "headers": {                     "content-type": "application/json"                   },                   "processdata": false,                   "data": "{\n  \"key\": \"key goes here\",\n\t\"message\":{\n\t\t\"subject\": \"test invitation\",\n\t\t\"from_email\": \"info@test.co.za\",\n\t\t\"from_name\": \"test group\",\n\t\t\"to\": [{\"email\": \"email.com\", \"name\": \"applicant\"}]\n\t},\n\t\"template_content\": \n\t[\n\t\t{\n\t\t\t\"name\": \"test\",\n\t\t\t\"content\": \"https://www.test.co.za/invite/\"\n\t\t},\n\t\t{\n\t\t\t\"name\": \"createaccount\",\n\t\t\t\"content\": \"test\" \n\t\t}\n\t],\n  \"template_name\": \"test-template\"\n}"                 }                 $.ajax(settings).done(function (response) {                   console.log(response);                 });           }else {             $("#result").text(" please enter valid email address.");             $("#result").css("color", "red");         }         return false;     } 

p.s : such issues use develop console check whats ging on code. can put console.log("comment/object") check @ point code breaking.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -