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 :: props to react router link 
Javascript :: disemvowel trolls codewars javascript 
Javascript :: js get file content from url 
Javascript :: netlify refresh page not found 
Javascript :: how to wait in javascript 
Javascript :: how to find factorial of a number using Recursion in javascript 
Javascript :: Javascript prime number check 
Javascript :: javascript check radio button 
Javascript :: remove space from string javascript 
Javascript :: download image jquery onclick 
Javascript :: get full date in javascript 
Javascript :: show hide boxes using radio button selection jquery 
Javascript :: validatorjs get all errors 
Javascript :: javascript adding delay 
Javascript :: javascript thousand separator 
Javascript :: checkbox on click jquery 
Javascript :: settimeout 
Javascript :: javascript number methods 
Javascript :: express search query template 
Javascript :: javascript random sort array 
Javascript :: have flat list automatically wrap react native 
Javascript :: how to validate age above 18 in javascript 
Javascript :: convert iso 8601 to utc javascript 
Javascript :: js array sum 
Javascript :: discord.js random message 
Javascript :: package json add git repo 
Javascript :: rounding off in javascript 
Javascript :: getkey by value js 
Javascript :: javascript backticks 
Javascript :: js array sort 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =