How to remove required filed

541 Views Asked by At

I'm new to coding and I want to know how to remove the required field in this code. Thank you. For example when the fields are empty I can submit without any error showing.

>      function valid_inf() {   var errorUsername = document.getElementById('first_name-error');   var usernameInput =
> document.getElementById('first_name');   var errphone =
> document.getElementById('phone-error');   var phoneInput =
> document.getElementById('phone');   var erroremail =
> document.getElementById('email-error');   var emailInput =
> document.getElementById('email');   var firstname =
> $('#first_name').val();   var phone = $('#phone').val();   var email =
> $('#email').val();   if (firstname == "") {
>     errorUsername.style.display = 'block';
>     usernameInput.className = 'form__input form__input--red rounded-4';
> 
>   }else{
>     $("#first_name").on('keyup', function(){
>      errorUsername.style.display = 'none';
>      usernameInput.className = 'form__input rounded-4';    });
> 
>   }   if (phone == "") {
>     errphone.style.display = 'block';
>     phoneInput.className = 'form__input form__input--red rounded-4';
> 
>   }else{
>     $("#phone").on('keyup', function(){
>      errphone.style.display = 'none';
>      phoneInput.className = 'form__input rounded-4';    });
> 
>   }   if (email == "") {
>     erroremail.style.display = 'block';
>     emailInput.className = 'form__input form__input--red rounded-4';
>     return false;   }else{
>     $("#email").on('keyup', function(){
>      erroremail.style.display = 'none';
>      emailInput.className = 'form__input rounded-4';    });
1

There are 1 best solutions below

0
On

if you want to remove validation you just remove the if-else condition of that particular field name

For example, if you want to remove first_name validation, you need to change your code like this

 function valid_inf() {
var errorUsername = document.getElementById('first_name-error');
var usernameInput =
    document.getElementById('first_name');
var errphone =
    document.getElementById('phone-error');
var phoneInput =
    document.getElementById('phone');
var erroremail =
    document.getElementById('email-error');
var emailInput =
    document.getElementById('email');
var firstname =
    $('#first_name').val();
var phone = $('#phone').val();
var email =
    $('#email').val();

// if (firstname == "") {
//     errorUsername.style.display = 'block';
//     usernameInput.className = 'form__input form__input--red rounded-4';

// } else {
//     $("#first_name").on('keyup', function () {
//         errorUsername.style.display = 'none';
//         usernameInput.className = 'form__input rounded-4';
//     });

// }
if (phone == "") {
    errphone.style.display = 'block';
    phoneInput.className = 'form__input form__input--red rounded-4';

} else {
    $("#phone").on('keyup', function () {
        errphone.style.display = 'none';
        phoneInput.className = 'form__input rounded-4';
    });

}
if (email == "") {
    erroremail.style.display = 'block';
    emailInput.className = 'form__input form__input--red rounded-4';
    return false;
} else {
    $("#email").on('keyup', function () {
        erroremail.style.display = 'none';
        emailInput.className = 'form__input rounded-4';
    });