javascriptjquery-form-validator

JavaScript Multiple Entry Form Validation


I am trying to use Javascript to validate user input before page redirects. Users can either Input Student ID, Name, or Gender, and based on their input, they will be redirected to a URL.

However, I don't seem to get the multiple entries correctly in my javascript and nothing happened when the submit button is clicked.

I have tried different solutions which I found here.

see my JavaScript code below;

var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("studentid").value;

if ( studentid == "12345" || studentid == "Daniel" || studentid == "Boy"){
alert ("Correct Input");
window.location = "https://www.google.com"; 
// Redirecting to other page.

return false;
}

else{
attempt --;// Decrementing by one.
alert("ATTENTION!\nInvalid student ID!\nNot associated with any student\nYou have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("studentid").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}

I have tried to use the solutions below;

if ( studentid == "#12345, #Daniel, #Boy"));{
alert ("correct input");
window.location = "https://www.google.com"; 
// Redirecting to other page.


if ( studentid == '12345', 'Daniel', 'Boy'){
alert ("correct input");
window.location = "https://www.amazon.com"; 
// Redirecting to other page.


Solution

  • After so many attempts, I finally got it right!

    var attempt = 3;
    function check(form)
    {
    if(form.studentid.value == "12345" || form.studentid.value == "DANIEL" || form.studentid.value == "BOY")
      {
        window.location.replace('https://www.google.com')
    
    return false;
    }
     else
     {
       attempt --;// Decrementing by one.
    alert("ATTENTION!\nInvalid student ID!\nNot associated with any student!\nYou have left "+attempt+" attempt;");
    // Disabling fields after 3 attempts.
    if( attempt == 0){
    document.getElementById("studentid").disabled = true;
    document.getElementById("submit").disabled = true;
    return false;
    }
    }
    }