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

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>
<?php
    $_POST['lowPrice']=preg_replace('/[^0-9\.\-]+/','',$_POST['lowPrice']);

//...

?>

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

You can use the regular expression ^[0-9]+(\.[0-9]{2})?$ to check whether the input is in money format or not.

You can use the following code in php to check whether the input matches the above regular expression:

if (preg_match('/^[0-9]+(\.[0-9]{2})?$/', $_POST['lowPrice'])) {
    # Successful match
} else {
    # Match attempt failed
}

Do this for all the validation you need to do for checking whether the input is in money format or not.

The description of the regular expression is given below:

^ : Assert position at the beginning of the string 
[0-9]+ : Match a single character in the range between "0" and "9" 
   + : Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
(\.[0-9]{2})? : Match the regular expression below and capture its match into backreference number 1 
   ? : Between zero and one times, as many times as possible, giving back as needed (greedy) 
   \. : Match the character "." literally 
   [0-9]{2} : Match a single character in the range between "0" and "9" 
      {2} : Exactly 2 times 
$ : Assert position at the end of the string (or before the line break at the end of the string, if any)