douzhang1115 2014-02-17 02:43
浏览 77
已采纳

验证输入字段只包含数字和小数(货币格式)[关闭]

I know this prob a really simple question.. I just need a pay to validate a text input field on my form. I know I can use the number type for HTML5 and have looked at prey_replace but I don't understand that function for the life of me..

What is the best way for me to validate a input field in my form? PHP, JQuery, Other? Just want to make sure what is inserted in the text field is in money format like 00.00 (I have set the maxlength of the text input to 5) I just need to check now to make sure only numbers or number and decimal is the only thing submitted when the form submits..

This may be overkill for what I need it for but I did find this fiddle from another question: http://jsfiddle.net/vY39r/11/ However I can't get it to work and I made sure the names match.

Here is a basic example of what I have:

<form name="setPrices" action="" method="POST">

<fieldset>
<label for="lowPrice">Low Resolution:</label>
<input type="text" class="price" id="lowPrice" name="lowPrice" value="<?php echo $low_price; ?>" maxlength="5" />
</fieldset>

<fieldset>
<label for="mediumPrice">Medium Resolution:</label>
<input type="text" class="price" id="mediumPrice" name="mediumPrice" value="<?php echo $medium_price; ?>" maxlength="5" />
</fieldset>

<fieldset>
<label for="highPrice">High Resolution:</label>
<input type="text" class="price" id="highPrice" name="highPrice" value="<?php echo $high_price; ?>" maxlength="5" />
</fieldset>

<input type="hidden" name="submitted" id="submitted" value="true" />

<button type="submit" id="submit" class="button">Save</button>
</form>
  • 写回答

2条回答 默认 最新

  • douzhangli9563 2014-02-17 02:48
    关注
    <?php
        $_POST['lowPrice']=preg_replace('/[^0-9\.\-]+/','',$_POST['lowPrice']);
    
    //...
    
    ?>
    

    Edit: Capitalised your P in lowPrice for clarity...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler