您现在的位置是:首页 > 电脑技术查询 > web开发

asp.net中判断是不是为数字

编辑:chaxungu时间:2022-10-10 23:23:09分类:web开发

asp.net中判断是否为数字

运行正则表达式,可以判断用户的输入是否为数字。

C#代码(后台):

using System.Text.RegularExpressions;public bool IsNumberic(string str)         {             Regex   reg=new   Regex("^(([0-9]+.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*.[0-9]+)|([0-9]*[1-9][0-9]*))$");               Match   ma=reg.Match(str);               if(ma.Success)               {                   //是数字                 return true;             }               else               {                   //不是数字                   return false;             }        }

javascript 代码(前台):
    if(document.getElementById('mnyPrice').value!='' && document.getElementById('mnyPrice').value!='0')    {        var objRe = /^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/;        if(!objRe.test(document.getElementById('mnyPrice').value))        {            alert('单价只能为大于或等于0数字');            form1.mnyPrice.focus();            return false;        }    }