Posted on : 2020-12-02 14:54:56 View Type: public
Ronald Capodagli

How do I allow negative number in the quantity fields, at least the last one?
How do I allow negative number in the quantity fields, at least the last one?
Please explain the reason of negative number in Quantity field and what it will do for you?
Sometimes you need to issue a credit for a part returned or a service performed.
Hey the expression REGEX
var valid = /^\d{0,8}(\.\d{0,2})?$/.test(this.value),
Allows only numbers with a decimal following 2 digits only. And maximum allows 8 numbers. Which i have replaced now to allow a negative number as well without any digit limit after decimal as well. So kindly open file computer-repair-shop >> assets >> admin >> js >> my-admin.js
and replace all code with attached.
// JavaScript Document
(function($) {
"use strict";
//calling foundation js
jQuery(document).foundation();
jQuery.fn.exists = function(){ return this.length > 0; }
jQuery(document).on("keyup", "#wc_price, #wc_cost, .wc_validate_number", function(){
var valie = /^-?[0-9]\d*(\.\d+)?$/.test(this.value),
//var valid = /^\d{0,8}(\.\d{0,2})?$/.test(this.value),
val = this.value;
if(!valid){
console.log("Invalid input!");
this.value = val.substring(0, val.length - 1);
}
});
jQuery(document).ready(function() {
$('#customer, #technician, #select_rep_products, #select_rep_services, #job_technician, #job_customer').select2();
if($('#updateUserFormReveal').exists()) {
$('#updateUserFormReveal').foundation('open');
}
});
})(jQuery); //jQuery main function ends strict Mode on
After updating file don't forget to refresh your cache and you would be able to have negative numbers as well thanks.
Thanks - I was on the right track with modifying the REGEX...
I replaced my-admin.js witht he script you provided.
When I type in the minus sign and hesitate slightly typing in the number and hit enter, the entry field immediately refreshes itself with the number without the minus sign.
When I type in the minus sign and don't type in the number and don't hit enter, the entry field immediately refreshes itself to BLANK.
If I type in the minus sign and immediately type in the number (like within a 1/5 second) - all works well when I hit enter.
Weird, any ideas?
Ron
Hey in Web browsers, When you leave leave the field then JavaScript runs and validates if the value in it is correct or not, if its not correct thn its automatically adding 1 in Quantity field.
So when you leave a Field by pressing TAB key or by pressing mouse button somewhere else outside that field or in any other input field, this is taken as Changed by JavaScript and validation is started.
If you press enter, then its submission of form no matter on which field you press enter, its taken as form submission. So you should use TAB and mouse to move around and enter should be used only when you want to submit or update the form.