Validating a text field containing a float is a valid percentage value?

后端 未结 3 1376
北海茫月
北海茫月 2021-01-29 05:00

I have a form text field that has a KeyUp event. On KeyUp I\'m ignoring anthing but numbers, the period, backspace, delete and cursor keys. So, the only thing in the field ca

3条回答
  •  野性不改
    2021-01-29 05:51

    try this expression: the 100% is a special case of a more generic pattern

    var re = /^((0|[1-9]\d?)(\.\d{1,2})?|100(\.00?)?)$/;
    

    explanation

    (0|[1-9]\d?) allows numbers from 0 to 99

    (\.\d{1,2})? allows a dot with 1 or 2 digits after

    | otherwise

    100 accept 100

    (\.00?)? with optional 0 or 00

    Edit:

    I tried this jsfiddle demo http://jsfiddle.net/pCDVn/1/ and it's working... look at the console

提交回复
热议问题