This question already has answers here:
checking whether textfield contains letters in javascript
(2 answers)
Closed 7 years ago.
I'm very new to JavaScript, and I am a little confused. How do I check if my input field:
<form name='formular' method='post' onsubmit='return atcheck()'>
E-mail <input type='text' name='email' id='em'>
<input type='submit' value='Submit'>
</form>
contains the symbol "#"? I'm not looking for a full good e-mail validation, I just wanna check if the field contains that symbol in particular when submitted (in the atcheck() function).
<script language="Javascript">
function atcheck(){
if(document.getElementById('em').value.indexOf('#') === -1) {
// No # in string
return false;
} else {
// # in string
return true;
}
}
</script>
Here's one way to accomplish that:
function atcheck() {
var has_at_char = document.getElementById("em").value.indexOf("#") > -1;
if (has_at_char) {
return false;
}
// your previously exisiting implementation of atcheck() could follow here
}
use indexOf() function
indexOf() documentation
to get the text on your function, you need to use document.getElementById("em").value
Related
This question already has answers here:
How do I select elements on multiple attribute values
(6 answers)
Closed 3 years ago.
Assuming you have a large number of inputs of type 'file', like so:
<input type="file" id="fileSomething1" />
<input type="file" id="fileSomething2" />
Is it possible to select all inputs of type 'file' that the user has loaded at least 1 file into?
I tried this, but it didn't work:
$("input[type='file' && value!='']").length
You have incorrect syntax for chaining of multiple attribute equals selector. It should be:
$("input[type='file'][value!='']").length
Working Demo
I would also suggest you to consider using .filter() selector for comparing value condition.
This code enable loop checking for input type file,If any of it find having value then it would print
$("#butn").on("click", function() {
$("input[type=file").each(function() {
if ($(this).val() != "") {
console.log($(this).attr('id'))
}
});
})
$("input[type=file").on("change", function() {
if ($(this).val() != "") {
console.log($(this).attr('id'))
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" id="fileSomething1" />
<input type="file" id="fileSomething2" />
<button id="butn">click</button>
This question already has answers here:
How do I remove all null and empty string values from an object? [duplicate]
(13 answers)
Closed 6 years ago.
I have a search form with several fields. If user types nothing into field, it still will be sent to the server with value "null".
However, there are about 25 optional (advanced) fields in this form, and if user types nothing into it, JSON shouldn't contain this field at all.
So, my question is: is there some way (pattern maybe) to avoid those 25 "if () else"? What is the best way to form that final JSON?
Thanks!
UPD:
Thank you, #RIYAJKHAN for your answer!
I've managed to solve it by myself using LoDash:
private compactObject(obj) {
let compactObj = this.removeEmptyProperties(_.cloneDeep(obj));
_.forEach(compactObj, (value, key) => {
if (_.isObject(value)) {
compactObj[key] = this.compactObject(this.removeEmptyProperties(compactObj[key]));
}
});
return this.removeEmptyProperties(compactObj);
};
private removeEmptyProperties(obj) {
let trimedObj = _.cloneDeep(obj);
_.forEach(trimedObj, (value, key) => {
if (!value || _.isEmpty(trimedObj[key])) {
delete trimedObj[key];
}
});
return trimedObj;
}
<form name="form" ng-submit="submit(form)">
<input type="text" name="test1" ng-model="test1">
<input type="text" name="test2" ng-model="test2">
<input type="text" name="test3" ng-model="test3">
<input type="button" name="submit">
</form>
JavaScript :
$scope.submit = function(form){
for(var prop in form){
if(!!!form[prop]){
delete form[prop];
}
}//form will contain form prop having only valid values
}
This question already has answers here:
Trim string in JavaScript
(20 answers)
Closed 8 years ago.
I have these scripts...
Name: <input type="text" id="inputName" onblur="verifyName(this.value)">
<script type="text/javascript">
function verifyName(nameInput)
{
if(nameInput=="")
{
// error
alert('please enter your name');
}
else
{
// accepted
}
}
</script>
This will display an error if the user don't want to enter his/her name on the textbox.
What if the user is lazy and he/she will enter " " (a space or more) as a name? This can be accepted by the JavaScript. It will return false on the if condition.
But I want the user not to enter only spaces. Is there any way to do that?
Note: Any number of spaces (without any letters) inputted are invalid.
If the user inputs one of the texts below, JavaScript should accepts/rejects the input as follows...
"John" --> valid
"" --> invalid
"John Doe" --> valid
" " --> invalid
"John Doe" --> valid (can be edited to remove too many spaces)
" Kim" --> valid (can be edited to remove too many spaces)
" " --> invalid
function verifyName(nameInput)
{
nameInput = nameInput.trim();
if(nameInput=="")
{
// error
alert('please enter your name');
}
else
{
// accepted
}
}
The trim() function will remove all spaces. If there are only spaces in the var, trim() with return "".
Trim the string before you do the checking. See below
if(nameInput.trim()=="")
{
// error
alert('please enter your name');
}
else
{
// accepted
}
One (of quite a few) possible implementation of your function uses regular expressions:
function verifyName(nameInput) {
return input.match(/\w{1,12}/) === null ? false : true;
// ^^
// Or some other sensible limitation to the length of a name.
}
You can make use of regular expression to replace all the spaces from the string and then check its length. If there is at least one character remaining after removing all the spaces - that's your valid case right?
testInput = function(){
var inpStr = $('#txtInp').val();
var teststr = inpStr.replace(/\s/g, "");
//$('#testStrDiv').text(teststr);
if((teststr.length)&&(teststr.length>0))
{
$('#divOutp').html('<b>Valid! </b>');
}
else{
$('#divOutp').html('<b>Invalid! </b>');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="txtInp" />
<input type="button" onclick="testInput()" value="test" />
<div id="testStrDiv" />
<div id="divOutp" />
This question already has answers here:
html "data-" attribute as javascript parameter
(6 answers)
Closed 8 years ago.
i need to send the input fields "data-validation" to javascript function.
form is
<input type="text" name="fname" id="fname" data-validation="length" onchange="validate(this);" required />
Javascript is
function validate(value)
{
var value=value.value;
var validation=value.data-validation;
alert(value +" "+ validation);
return false;
}
i can't get the value of data-validation in javascript.
please review this code
getAttribute() is used to get attribute of element
function validate(ele)
{
var value=ele.value;
var validation=ele.getAttribute("data-validation")
alert(value +" "+ validation);
return false;
}
Demo here http://jsfiddle.net/GgfM3/
Attribute of an element cannot be accessed just through the dot. You should use "getAttribute("myAttribute")" in javascript and "attr("myAttribute")" with jQuery.
How about using jQuery? The post here suggests that "onchange is only triggered when the control is blurred". The jQuery .on can monitor value changes in the input.
$("#fname").on("input", function(){
var value=$(this).val();
var validation=$(this).attr("data-validation");
alert(value +" "+ validation);
}
Check example here.
You can retrive the Attribute values using the getAttribute('value') method.
In you example you can get the value using
function validate(data)
{
var dataFromTextbox = data.getAttribute("data-validation")
alert(dataFromTextbox);
}
This question already has answers here:
jQuery: what is the best way to restrict "number"-only input for textboxes? (allow decimal points)
(40 answers)
Closed 9 years ago.
Need to restrict entering alphabets and special characters in a textbox using jquery. Need to enter only numbers.
How to implement using keypress() functionality in jquery for a text box in jsf?.
Code :
<table>
<tr>
<h:inputlabel value="Actual"></h:inputlabel>
<td>
<h:inputtext id="Actual" styleClass="input-tex" value="#bean.customer"></h:inputtext>
<td>
</tr>
<table>
Any help is appreciated.
you can try this in onkeypress function
function validateDigits(){
var str = "54665";
var re = /^\d+$/;
alert(str.match(re)!=null);
}
it returns true in case it validate successfully otherwise returns false
I'm sure there's a more elegant way, but I use:
$('#parentobject').on('keypress', '#inputid', function (e) {
if (isNaN(e.currentTarget.value) == true) {
var textlen = $(this).val().length;
$(this).val($(this).val().substring(0, (textlen - 1)));
return false;
}
});