Input text validation jquery - javascript

I'm using Jquery Framework, I'm trying to validate input data, which is not equal to null or empty string. My approach is to validate data in a single line as I'm maintaining cookies storage. My question is, how can I validate input text that value its not an empty string, similarly I checked radio buttons and check boxes that are checked. How can I validate input[type='text'] with same condition input[value !=''] in a single line.
Here's my code:
_strFormElements = "input[type='text']," +
"input[type='checkbox']:checked," +
"input[type='radio']:checked," +
"input[type='image']," +
"input[type='file']," +
"select," +
"textarea";
I then checking as follow, but I want to check and validate before theses checks as my init method creates empty feilds before this.
if (elem.is('input:text') || elem.is('input:hidden') || elem.is('input:image') ||
elem.is('input:file') || elem.is('textarea')) {
elem.val(value);
}
My Try:
I've tried input[type='text' value != ''] but I'm unable to check this in a single line.

Related

Javascript get all input elements that contain values [duplicate]

This question already has answers here:
Select inputs that doesn't have value attribute with querySelector
(2 answers)
querySelectorAll detecting value in input
(2 answers)
Closed 3 years ago.
I'm using the querySelectorAll() JS function to retrieve all elements under a datagrid table (aspx page) , that datagrid contains several checkboxes / textboxes , there's a save button which should only be enabled when the following rule is satisfied :
At least 1 checkbox needs to be checked OR at least one textbox needs to contain text ( not empty )
Is there a way to check if any of the textbox input elements returned by querySelectorAll() , has a value ?
function validate() {
var search = document.getElementById("myGrdID").children;
//var hasAnyText = search[0].querySelectorAll("input:valid");
var hasAnyChecked = search[0].querySelectorAll("input:checked");
console.log("Any check? " + hasAnyChecked.length);
}
i'd like to avoid using Jquery , already managed to get all the checked input elements , couldn't find a way yet to find all the textboxes with values (any value not a specific string).
Are you able to use the filter function ?
var results = hasAnyChecked.filter(input => input.value != null)
Edit : comment below me has a point, but if you use those kind of selectors you won't be able to check multiple scenarios :
var results = hasAnyChecked.filter(input => input.value !== null || input.value !== false) // Handles both text inputs and checkboxes

dynamic jquery validation not working correclty

hi i am trying to dynamically validate my registration form, so that check are being made, when the user enters values into input fields using jquery and php. i am returning the values using a json array, the first input box is validated but the second one does not get validated and im not sure why? any help would be mostly appreciated.
This problem is occurring because your validate() function takes two parameters: fname and name, but you are always only sending the first parameter and not the other one.
To fix it, in your fname events do this:
validate($('#fname').val() , "");
--------------- ---
| |
fname lname
and in your lname events do this:
validate("" , $('#lname').val());
--- ----------------
| |
fname lname
and in your PHP make sure you set it so that it reads no "blanks":
if( isset($_POST['lname']) && $_POST['lname'] !== "")
if( isset($_POST['fname']) && $_POST['fname'] !== "")

How to validate forms (which are generated dynamically)?

Can you please tell me how to do validation in a form which is generated dynamically? I am using one plugin dform.js which converts JSON to form. I am to do that validate of fields.
http://jsfiddle.net/Xe3FG/2/
I take help from this.
https://github.com/daffl/jquery.dform
In my demo, I take 2 number fields. If the user enters a string and go to next field, I need it to display an error in front of the field, "please enter only numbers." I need the same with second field.
Can we do only using drom.js or validation.js?
I am able to validate when user enters data in the field and then press enter.
So I used blur event. It is not a good practice to use blur event on every field. Can you give a different way and a good way to validate?
("#totalRetryCount").blur(function() {
// Number element type returns empty value when NaN
if ( $('#totalRetryCount').val() == '' )
alert('enter a number');
});
$("#totalRepeatCount").blur(function(event) {
// Number element type returns empty value when NaN
if ( $('#totalRepeatCount').val() == '' )
alert('enter a number');
I used these two blur events. I don't want to use these events. Can we do these validations another way?
I've already explained to you how to do this in your last question and provided you with a fully working example which you've seemed to completely ignore. In this case, you would just need to check if the value is an empty string, as that is the default value of a number type input field containing non-numeric data.
Fiddle demo
$("#testSuiteConfigurationform").validate(validateInputParameters());
function validateInputParameters() {
jQuery.validator.addMethod("onlyNumbers", function(value, element) {
return value != "";
}, " Please enter only numbers");
var validation = {
onfocusout : function(element) {
$(element).valid();
},
rules : {
totalRetryCount: { onlyNumbers: true }
},
};
return validation;
};

If the input field is blank, set a specific value

I'm designing a shopping cart system for my site. I have an input to enter the quantity for each item, with the value 1 automatically put in. The problem is, if someone clicks in the input and deletes the 1, leaving the field blank, then the item isn't added to the cart.
How can I set a 1 in the input field if it is blank, either using js or jquery? Or if the field is blank have the form automatically submit a 1?
Something like this should work:
$("#input_ID").blur(function() {
if($.trim($(this).val()) === "") {
$(this).val("1");
}
});
If it needs to be a group of inputs, simply give them a common class and change:
$("#input_ID").blur( . . .
. . . to:
$(".class_name").blur( . . .
var value = $(your_input).val();
if ( value == '' ) value = 1
The following solution is only js no jquery.
Within your internal function, wherever you are saving the input.
For example:
let itemQuantity = document.getElementById("item_quantity");
//you can also add account for space or other non numeric values (consider looking into regex)
if (itemQuantity === ""){
itemQuantity = 1
// or "1" depending on how you need to pass the values
return taskPriority;
}
alternate:
if (itemQuantity.value === ""){
itemQuantity = 1
// or "1" depending on how you need to pass the values
}
The first one will force the user to reset default to 1 and then add to cart again.
Second will by default set it to 1 if they input blank values.
that should allow you to set a default value.

Why doesn't this JavaScript evaluation work?

I only want to execure a ceratin code IF three input fields do not have empty values. So why doesn't this code work:
if($("#field1").val() != "" && $("#field2").val() != "" && $("#field3").val() != "")
Make sure the input fields are named properly? They should have the id attribute for them set.
Do all three inputs have ids matching your selectors? You may also want to check that the type of the values is not undefined like so:
if(typeof($('#field1').val())!='undefined' ...

Categories