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 :: mongodb $in regex 
Javascript :: hex string to int javascript 
Javascript :: boolean constructor js 
Javascript :: Not allowed to navigate top frame to data URL 
Javascript :: detect if two line segments intersect each other javascript 
Javascript :: Cookies In NodeJS 
Javascript :: jquery insert after element 
Javascript :: javascript truncate decimal without rounding 
Javascript :: Angular Laravel has been blocked by CORS policy: Request header field x-requested-with is not allowed by Access-Control-Allow-Headers in preflight response. 
Javascript :: express req body undefined 
Javascript :: change image src onclick javascript 
Javascript :: nuxt emit 
Javascript :: cannot use import statement outside a module 
Javascript :: Reverse a String With Built-In Functions 
Javascript :: Close popup window 
Javascript :: js to uppercase 
Javascript :: how to read breakline in html 
Javascript :: react.fragment react native 
Javascript :: how to check value is array or not in javascript 
Javascript :: font google expo 
Javascript :: react js loop through array of objects 
Javascript :: $lookup in mongodb 
Javascript :: JavaScript Use clearInterval() Method 
Javascript :: is a letter javascript 
Javascript :: typescript class constructor overload 
Javascript :: disable button in jsx 
Javascript :: javascript urlsearchparams to string 
Javascript :: A <Route is only ever to be used as the child of <Routes element, never rendered directly. Please wrap your <Route in a <Routes. 
Javascript :: redis set expire time node js 
Javascript :: how to replace strings with react components 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =