【发布时间】:2014-03-17 12:07:46
【问题描述】:
我正在寻找一个表单验证脚本(大概是 JavaScript?)。
我们希望这是一个输入文本字段,而不是下拉菜单。该公司提供定制切割各种长度的产品。最小订单为 0.5m,之后他们只想允许以0.05m 为增量的长度,如下所示:
仅允许两位小数点以0 或5 结尾的数字(如果存在)。
- 0.50(最小订单)
- 0.55
- 0.60
- 0.65
- 等等……
- 1.00
- 1.05
- 1.10
- 1.15
- 等等……
- 130.00
- 130.05
- 130.10
- 等等……
有什么想法吗?有什么建议吗?
更新:
我意识到解决方案需要合并到一些现有脚本中,这可能会使情况复杂化或简化?这是处理表单提交时的操作的代码。
<script type="text/javascript"><!--
$('select[name="profile_id"], input[name="quantity"]').change(function(){
$.ajax({
url: 'index.php?route=product/product/getRecurringDescription',
type: 'post',
data: $('input[name="product_id"], input[name="quantity"], select[name="profile_id"]'),
dataType: 'json',
beforeSend: function() {
$('#profile-description').html('');
},
success: function(json) {
$('.success, .warning, .attention, information, .error').remove();
if (json['success']) {
$('#profile-description').html(json['success']);
}
}
});
});
$('#button-cart').bind('click', function() {
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: $('.product-info input[type=\'text\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
dataType: 'json',
success: function(json) {
$('.success, .warning, .attention, information, .error').remove();
if (json['error']) {
if (json['error']['option']) {
for (i in json['error']['option']) {
$('#option-' + i).after('<span class="error">' + json['error']['option'][i] + '</span>');
}
}
if (json['error']['profile']) {
$('select[name="profile_id"]').after('<span class="error">' + json['error']['profile'] + '</span>');
}
}
if (json['success']) {
$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
$('.success').fadeIn('slow');
$('#cart-total').html(json['total']);
$('html, body').animate({ scrollTop: 0 }, 'slow');
}
}
});
});
//--></script>
【问题讨论】:
-
尝试搜索字符类。还有锚。还有一个关于正则表达式的教程。
-
数字是浮点数还是字符串?这对于该方法非常重要...
-
0.00 会满足 moob
-
有人向我建议您可以使用汇总功能?因此,如果用户输入 1.03,它会四舍五入到 1.05,如果他们输入 1.17,它会四舍五入到 1.20 - 想法?
标签: javascript regex forms validation