Issue
I have tried to get an element called price input of an html form for jQuery without a success:
// HTML codes:
<input type="text" id="priceID" name="price">
// jQuery codes:
jQuery(document).ready(function($){
// jquery('#priceID').on("keyup", function(){
jquery('#priceID').on("keyup", function(){
// var priceStr = jquery('#priceID').val();
var priceStr = document.getElementById('priceID');
// var priceStr = document.getElementsByTagName("INPUT");
var valid = /^\d{0,4}(\.\d{0,2})?$/.test(priceStr),
val = priceStr.value;
if(!valid){
console.log("Invalid input!");
priceStr.value = val.substring(0, val.length - 1);
}
});
});
I use this guidance as a reference for testing the input string/value of the price field above:
Price field validation using javascript either jquery?
I have tried the above jQuery codes for a form at Wordpress Admin backend. The html form is located somewhere in child theme. That may be an issue?
Please advise how to get the price value in a correct way.
Cheers
Solution
FIXED:
I just insearted a line of code below:
jQuery('#priceID').attr('required','required');
on top inside the function created by Professor Abronsius to get attention from my wp theme (haha), and now the string entered in the Price field can be retrieved.
jQuery(document).ready(function($) {
jQuery('#priceID').on("keyup", function() {
jQuery('#priceID').attr('required','required');
/*
within the callback you can access $(this) to refer to the element
and you can find the value of the input using val()
*/
var price = $(this).val();
var valid = /^\d{0,4}(\.\d{0,2})?$/.test(price);
if (!valid) {
console.log("Invalid input!");
/* Using val() again to set the modified value */
$(this).val(price.substring(0, price.length - 1));
}
});
});
Cheers
Answered By - daro2013
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.