Improve JS and fix decimal validation

Fixes #7996
This commit is contained in:
Mike Jolley 2015-04-23 13:35:27 +01:00
parent 4997ed0efb
commit 3f648beb1f
2 changed files with 66 additions and 102 deletions

View File

@ -5,117 +5,81 @@
*/
jQuery( function ( $ ) {
// Price input validation
$( document.body ).on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
$('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } );
return this;
});
// Field validation error tips
$( document.body )
.on( 'wc_add_error_tip', function( e, element, error_type ) {
var offset = element.position();
$( document.body ).on('keyup change', '.wc_input_price[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
if ( value !== newvalue ) {
$(this).val( newvalue );
if ( $(this).parent().find('.wc_error_tip').size() == 0 ) {
var offset = $(this).position();
$(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_mon_decimal_error + '</div>' );
$('.wc_error_tip')
.css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + $(this).height() )
if ( element.parent().find('.wc_error_tip').size() == 0 ) {
element.after( '<div class="wc_error_tip ' + error_type + '">' + woocommerce_admin[error_type] + '</div>' );
element.parent().find('.wc_error_tip')
.css('left', offset.left + element.width() - ( element.width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + element.height() )
.fadeIn('100');
}
}
return this;
});
})
.on( 'wc_remove_error_tip', function( e, element, error_type ) {
element.parent().find('.wc_error_tip.' + error_type).remove();
})
.on( 'click', function() {
$('.wc_error_tip').fadeOut('100', function() { $(this).remove(); } );
})
.on( 'blur', '.wc_input_decimal[type=text], .wc_input_price[type=text], .wc_input_country_iso[type=text]', function() {
$('.wc_error_tip').fadeOut('100', function() { $(this).remove(); } );
})
.on( 'keyup change', '.wc_input_price[type=text]', function() {
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%\\" + woocommerce_admin.mon_decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
$( document.body ).on('keyup change', '.wc_input_decimal[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%.\\" + woocommerce_admin.decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
if ( value !== newvalue ) {
$(this).val( newvalue );
if ( $(this).parent().find('.wc_error_tip').size() === 0 ) {
var offset = $(this).position();
$(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_decimal_error + '</div>' );
$('.wc_error_tip')
.css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + $(this).height() )
.fadeIn('100');
if ( value !== newvalue ) {
$(this).val( newvalue );
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_mon_decimal_error' ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_mon_decimal_error' ] );
}
}
return this;
});
})
.on( 'keyup change', '.wc_input_decimal[type=text]', function() {
var value = $(this).val();
var regex = new RegExp( "[^\-0-9\%\\" + woocommerce_admin.decimal_point + "]+", "gi" );
var newvalue = value.replace( regex, '' );
$( document.body ).on( 'keyup', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
var sale_price_field = $(this);
if( sale_price_field.attr('name').indexOf('variable') !== -1 ) {
var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]');
} else {
var regular_price_field = $('#_regular_price');
}
var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) );
var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) );
if( sale_price >= regular_price ) {
if ( $(this).parent().find('.wc_error_tip').size() === 0 ) {
var offset = $(this).position();
$(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18_sale_less_than_regular_error + '</div>' );
$('.wc_error_tip')
.css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + $(this).height() )
.fadeIn('100');
if ( value !== newvalue ) {
$(this).val( newvalue );
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_decimal_error' ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_decimal_error' ] );
}
} else {
$('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } );
}
return this;
});
$( document.body ).on( 'change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function(){
var sale_price_field = $(this);
})
.on( 'keyup change', '.wc_input_country_iso[type=text]', function() {
var value = $(this).val();
var regex = new RegExp( '^([A-Z])?([A-Z])$' );
if( sale_price_field.attr('name').indexOf('variable') !== -1 ) {
var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]');
} else {
var regular_price_field = $('#_regular_price');
}
var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) );
var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) );
if( sale_price >= regular_price ) {
sale_price_field.val( regular_price_field.val() );
} else {
$('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } );
}
return this;
});
$( document.body ).on('keyup change', '.wc_input_country_iso[type=text]', function(){
var value = $(this).val();
var regex = new RegExp( '^([A-Z])?([A-Z])$' );
if ( ! regex.test( value ) ) {
$(this).val( '' );
if ( $(this).parent().find('.wc_error_tip').size() === 0 ) {
var offset = $(this).position();
$(this).after( '<div class="wc_error_tip">' + woocommerce_admin.i18n_country_iso_error + '</div>' );
$('.wc_error_tip')
.css('left', offset.left + $(this).width() - ( $(this).width() / 2 ) - ( $('.wc_error_tip').width() / 2 ) )
.css('top', offset.top + $(this).height() )
.fadeIn('100');
if ( ! regex.test( value ) ) {
$(this).val( '' );
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18n_country_iso_error' ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18n_country_iso_error' ] );
}
}
return this;
});
})
.on( 'keyup change', '#_sale_price.wc_input_price[type=text], .wc_input_price[name^=variable_sale_price]', function() {
var sale_price_field = $(this);
$("body").click(function(){
$('.wc_error_tip').fadeOut('100', function(){ $(this).remove(); } );
});
if( sale_price_field.attr('name').indexOf('variable') !== -1 ) {
var regular_price_field = sale_price_field.parents('.variable_pricing').find('.wc_input_price[name^=variable_regular_price]');
} else {
var regular_price_field = $('#_regular_price');
}
var sale_price = parseFloat( accounting.unformat( sale_price_field.val(), woocommerce_admin.mon_decimal_point ) );
var regular_price = parseFloat( accounting.unformat( regular_price_field.val(), woocommerce_admin.mon_decimal_point ) );
if ( sale_price >= regular_price ) {
$( document.body ).triggerHandler( 'wc_add_error_tip', [ $(this), 'i18_sale_less_than_regular_error' ] );
} else {
$( document.body ).triggerHandler( 'wc_remove_error_tip', [ $(this), 'i18_sale_less_than_regular_error' ] );
}
});
// Tooltips
var tiptip_args = {

File diff suppressed because one or more lines are too long