正数表示负数和正数 [英] Regex for negative and positive numbers

查看:91
本文介绍了正数表示负数和正数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个用于输入字段的正则表达式,以允许使用负数和正数,并且不能使用字母/特殊字符.我见过其他人也做到了,但是他们在字符串的任何位置都允许减号,而我只想在字符串的开头允许减号.

I need a regex for an input field to allow negative and positive numbers and no alphabet/special characters. I have seen other people achieve this but they allow the minus sign at any point in the string whereas I only want to allow the minus sign at the beginning of the string.

注意:我想允许用户使用箭头,del,home键等.

Note: I want to allow the user to use the arrow, del, home keys etc.

推荐答案

此正则表达式获取正数和负数:

This the regex to get positive and negative numbers :

^-?\d+$

如果您想使用十进制数字,则可以使用:

If you want decimal numbers you can use this :

^-?\d+(\.\d{1,2})?$

和处的参数{1,2}用于十进制数字的精度.

The parameter {1,2} at the and is for precision of your decimal number.

这篇关于正数表示负数和正数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆