Home > Blockchain >  How can a form accept only a particular data type?
How can a form accept only a particular data type?

Time:03-22

So I have a form that gets student ID and Name, stores it in a JSO file then displays it automatically when a button is clicked . But now I want the form to filter the response of the students such that a student can only enter integers for the id and string for the name


async function addStudent() {
    const url = server   '/students';
    const student = {id: studentId, name: studentName};
    const options = {
        method: 'POST',
        headers: {
            'Content-Type' : 'application/json'
        },
        body: JSON.stringify(student)
    }
    const response = await fetch(url, options);
}

I tried googling everywhere but I couldn't find anything

CodePudding user response:

When your button is pressed, I would have your values be checked before going through your post request. Something formatted like the following:

if (isNaN(form.studentId.value) && !(isNaN(form.name.value))) {
    addStudent();
} else {
    alert("Invalid form!");
}
  • Related