简体   繁体   中英

How do I validate JSON data in a form

I have a HTML form that i am filling out with JSON data once a bit of that data is called, there are also 2 sets of data in my JSON file.

I am trying to make my error alert appear when the string of data ( in this circumstance it is a reference number ) is incorrect however, everytime I type in one of the reference numbers, the error message appears because the other one isnt being called, can anyone help me with this?

Here is my JQuery:

  $("#clickme").click(function () { $.getJSON("insurance.json", function (obj) { $.each(obj, function (key, value) { //Makes sure Search matches a reference number if ($("#search").val() == value.InsuranceReferanceNumber) { $(".hide").addClass("show"); $(".hide").removeClass("hide"); $(".ins").hide(); // Inputs values into form $("#firstname").val(value.Firstname); $("#lastname").val(value.Lastname); $("#address").val(value.Address); $("#phone").val(value.PhoneNumber); $("#dob").val(value.DOB); $("#email").val(value.Email); $("#carmanf").val(value.CarManufacturer); $("#model").val(value.CarModel); $("#year").val(value.Year); $("#fuel").val(value.FuelType); $("#engine").val(value.EngineSize); $("#cover").val(value.TypeofCover); $("#bonus").val(value.NoClaimsBonus); $("#paid").val(value.Paid); $("#amount").val(value.Amount); } else { alert("Error"); } }); }); }); 

Here is the JSON file, it is filled with false data.

 [{ "InsuranceReferanceNumber": "CAR123" , "Firstname": "Jerry" , "Lastname": "Ford" , "Address": "1 Brick Lane" , "PhoneNumber": "077123456" , "DOB": "12/08/1976" , "Email": "jerry.ford@mail.com" , "CarManufacturer": "Audi" , "CarModel": "R8" , "Year": 2017 , "FuelType": "Diesel" , "EngineSize": 5.2 , "TypeofCover": "Third Party " , "NoClaimsBonus": "3 Years" , "Paid": "Annually" , "Amount": 1800 } , { "InsuranceReferanceNumber": "CAR456" , "Firstname": "Danny" , "Lastname": "Beach" , "Address": "23 Oaklands Dr" , "PhoneNumber": "077654321" , "DOB": "09/11/1996" , "Email": "danny.beach@mail.com" , "CarManufacturer": "Vauxhall" , "CarModel": "Corsa" , "Year": 2011 , "FuelType": "Petrol" , "EngineSize": 1.2 , "TypeofCover": "Comprehensive" , "NoClaimsBonus": "2 Years" , "Paid": "Monthly" , "Amount": 800 } ] 

I'm not sure if I correctly understood your question, but I''ll try to help. You have logical operation inside '$.each' cycle and when

$("#search").val() == value.InsuranceReferanceNumber 

is incorrect, your code fires alert. However, this code will show alert almost every time actually, so I think you have to change your code with this:

  $("#clickme").click(function () { $.getJSON("insurance.json", function (obj) { var found=false; $.each(obj, function (key, value) { //Makes sure Search matches a reference number if ($("#search").val() == value.InsuranceReferanceNumber) { $(".hide").addClass("show"); $(".hide").removeClass("hide"); $(".ins").hide(); // Inputs values into form $("#firstname").val(value.Firstname); $("#lastname").val(value.Lastname); $("#address").val(value.Address); $("#phone").val(value.PhoneNumber); $("#dob").val(value.DOB); $("#email").val(value.Email); $("#carmanf").val(value.CarManufacturer); $("#model").val(value.CarModel); $("#year").val(value.Year); $("#fuel").val(value.FuelType); $("#engine").val(value.EngineSize); $("#cover").val(value.TypeofCover); $("#bonus").val(value.NoClaimsBonus); $("#paid").val(value.Paid); $("#amount").val(value.Amount); found=true; return false; } }); if(!found) alert("Error"); }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM