时卸下的toLocaleString格式化我有一个表单字段(字符串)为此我加入了“空间”千个分隔符使用这个脚本:检查现场验证
<script type="text/javascript">
// https://stackoverflow.com/questions/19470499/how-to-format-input-box-text-as-i-am-typing-it
jQuery(document).ready(function($) {
$("#input_10").on('keyup', function(){
var n = parseInt($(this).val().replace(/\D/g,''),10);
$(this).val(n.toLocaleString());
});
</script>
所以用户类型:“100000”,它将显示“100 000”
稍后,在表单提交过程中,我有一个PHP语句来验证该字段是否包含数值。
现在问题来了($值是格式字段的值):
- is_numeric($值)是不正确的,由于“空间”千个分隔符,所以我试图消除空间,但:
- is_numeric(str_replace函数(””,‘’,$值))也是不正确的,不通过的toLocaleString
删除添加的空间怎样删除已jQuery中添加了的toLocaleString格式什么时候在PHP中进行字段验证?
编辑: PHP代码中使用:
add_filter('gform_field_validation_1_10', 'bja_gf_custom_validation10', 10, 4);
function bja_gf_custom_validation10($result, $value, $form, $field) {
$formatedNumber = str_replace(' ', '', $value);
if ($result['is_valid'] && !is_numeric($formatedNumber)) {
$result['is_valid'] = false;
$result['message'] = 'formatedNumber:' . $formatedNumber;
}
return $result;
}
谢谢!
'is_numeric(str_replace(“”,“”,“100 000”));'是'true'。告诉我们你的PHP代码。 – JustBaron
是的,但问题是toLocaleString可能添加了一个不是**空格的char。 如果我输入 “100000”(其呈现为 “100 000”),然后执行一个PHP: 'ORD(SUBSTR($值,4,1));' 我接收的ASCII码160 .. 。 – SeriousBen74
也许看看从PHP字符串中删除ASCII字符:https://stackoverflow.com/a/1176923/715105 – JustBaron