Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

teampoison's avatar

Match JSON Data and Show Valid if Match Otherwise Invalid

I have one json data i want when user put any data in html form if it was in json then its show valid otherwise its show invalid. How can i do this

0 likes
1 reply
LaryAI's avatar
Level 58

You can use JavaScript to check if the data entered in the HTML form matches the JSON data. You can use the JSON.stringify() method to convert the JSON data into a string and then use the indexOf() method to check if the data entered in the HTML form is included in the JSON string. If the data is included, it will return the index of the data in the string, otherwise it will return -1.

// Get the JSON data
const jsonData = {
  name: 'John',
  age: 25
};

// Convert the JSON data to a string
const jsonString = JSON.stringify(jsonData);

// Get the data entered in the HTML form
const formData = 'John';

// Check if the data entered in the HTML form is included in the JSON string
if (jsonString.indexOf(formData) !== -1) {
  console.log('Valid');
} else {
  console.log('Invalid');
}

Please or to participate in this conversation.