Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

check if input is valid

//Get the input element from the DOM 
const inputElement = document.querySelector(".input")
//Get the object of ValidityState and log it to our console
console.log(inputElement.validity)
//Note that in the ValidityState Object we have a row called "valid" with boolean value
//In this way we can check if our input is valid or not AKA "true" || "false"
inputElement.addEventListener("input", function(){
  if(inputElement.validity.valid){
    consloe.log("Valid input")
  }else{
     consloe.log("Invalid input")
  }
})
Comment

check if input is valid js

//there are two ways to check the validity.

    inputElement.checkValidity() //returns true or false
    inputElement.validity //returns the validity-state object. 
    inputElement.validity.valid //returns true/false
Comment

how to check if input is valid javascript

// input if empty in bootstrap but can be used even without bootstrap
// In your html file
// take note id="frm_subscription" and required class be in all of your required inputs
  <form id="frm_subscription">
    <div class="form-group">
      <div class="row">
        <div class="col-md-6 col-xs-12">
          <input type="text" class="form-control form-input border required" name="business_name" placeholder="Business Name *" required="true">
        </div>
		<div class="col-md-6 col-xs-12">
          <input type="text" class="form-control form-input" name="business_address" placeholder="Business Address">
        </div>
	</div>
	</div>
</form>

// In your js
    function validate(formId) {
        let valid = true;
        $.each(formId, function(i, field) {
            $("[name='" + field.name + "']").prev('.text-danger').remove();

            if (field.value == "" && $("[name='" + field.name + "']").prop('required')) {
                valid = false;
              	// you can customise your error styles based on your theme
                // Below couple of lines are example used in bootstrap
                $("[name='" + field.name + "']").css({ "border": "1px solid #ff000087" });
                $("[name='" + field.name + "']").before('<span class="text-danger">This field is required *</span>');
            }
        });
        return valid;
    }

	// usage : if false dont continue
 	if (!validate($("#frm_subscription").serializeArray())) { return; }
Comment

PREVIOUS NEXT
Code Example
Javascript :: fastify testing 
Javascript :: ajax stand for 
Javascript :: javascript random item of array 
Javascript :: js remove several elements from array 
Javascript :: how to remove the elements from array and how to replace a new element in javascript 
Javascript :: rxjs coding example 
Javascript :: JSON parse error: Cannot deserialize value of type `java.util.Date` from String 
Javascript :: star print in javascript 
Javascript :: got back to start of for loop js 
Javascript :: p5js right mouse button released 
Javascript :: favicon express js 
Javascript :: using settings_data.json shopify 
Javascript :: activate router angular 
Javascript :: javascript check number length 
Javascript :: how to loop elements in javascript for of loop 
Javascript :: vuejs cordoba pantalla en blanco 
Javascript :: money formatting javascript 
Python :: how to avoid deprecation warning in python 
Python :: opencv show image jupyter 
Python :: matplotlib dark mode 
Python :: vowel and consonant list python 
Python :: python datetime tomorrow date 
Python :: install fastapi conda 
Python :: zsh: command not found: virtualenv 
Python :: pip install error 
Python :: how to automatically copy an output to clipboard in python 
Python :: how to get file name without extension in python 
Python :: incognito mode in selenium 
Python :: how to install dask in python 
Python :: print traceback python 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =