Preg match to validate only numbers integer or decimals with sign + or -

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Preg match to validate only numbers integer or decimals with sign + or -



Preg match to validate only whole numbers or decimals with sign + or -



How can I create a preg_match that only accepts decimal numbers which can have (or have no) + or - just when the number starts?



I put the following examples:



5 true



4.4 true



+5 true



+4.4 true



-5 true



-4.4 true



4.4- false



4.4+ false



For now I have a preg_match function that allows me to achieve this, but the problem is that I can enter the signs (. + -) in any part of the number, and I just need the signs (+ -) to be at the beginning and the point if it is decimal.


preg_match('/^[0-9.+]+$/', $value)



Thanks friends.





You may use: /^[+-]?d*.?d+$/ or see this: regex101.com/r/jkaSiN/1
– anubhava
Aug 7 at 20:07



/^[+-]?d*.?d+$/





@anubhava It was just what I needed. Thank you for your help friend. Regards
– Cristian Meza
Aug 7 at 20:09




1 Answer
1



Converting my comment to answer.



You may use this regex for making + or - optional at the start.


+


-


^[+-]?d*.?d+$



RegEx Demo



Regex Explanation:


^


[+-]?


+


-


d*.?d+


$






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered