Firstly I wanna check the given value in textfield is Number or not.
But I have number of textfields ,the number is depending upon dynamic value from php&yii like as follows
<?php foreach($model->getData() as $donation){ ?>
Donation For <?php echo $donation['DonationName'];?>
<label><b>$</b> <input class="textfld3" style="width:75%" title="First Name" placeholder="Amount" type="text">
</label>
<?php } ?>
so I wanna get the value of the textfield or textfields in js..
I had also used the jsfunction as
var texts= $(".textfld3").map(function() {
return $(this).val();
}).get();
alert(texts);
but all the values are showing in alert box along with commas
how should i get the value of textfiled or textfields to check whether it is number or not in js.
Loop through all the textfield using $(".textfld3").each(function() {
and find value of textfield one after another. If a textfield having a non numeric value then show alert message and will focus on that textfield.
$(".textfld3").each(function() {
var currentVal = $(this).val();
if(isNaN(currentVal)){
alert("Please enter numeric value");
$(this).focus();
return false;
}
});
From a quick view into your code, it looks to me, texts
is an array, so you could run through this array
var texts= $(".textfld3").map(function() {
return $(this).val();
}).get();
for ( var i = 0; i < texts.length; i++ ) {
alert(texts[i]);
}
You have to pass key
and value
parameter in map function so you can return value of all text fields by value, here ele
variable is value which is reference one element of that class selector
$("#btn").click(function(){
var texts= $(".textfld3").map(function(key,ele) {
return $(ele).val();
//return ele.value
}).get();
console.log(texts);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label><b>$</b> <input class="textfld3" style="width:75%" title="First Name" placeholder="Amount" type="text">
</label>
<hr>
<label><b>$</b> <input class="textfld3" style="width:75%" title="First Name" placeholder="Amount" type="text">
</label>
<hr>
<label><b>$</b> <input class="textfld3" style="width:75%" title="First Name" placeholder="Amount" type="text">
</label>
<hr>
<label><b>$</b> <input class="textfld3" style="width:75%" title="First Name" placeholder="Amount" type="text">
</label>
<hr>
<button id="btn"> Get Values</button>
</div>