求VB的正则表达式判断格式

来源:百度知道 编辑:UC知道 时间:2024/05/15 07:57:01
ASP判断一个用户名输入的是不是字母、数字和下划线,如果不是就弹出一个警告框,在JS里面我用:
if(!/^\w+$/.test(uname)) //用户名只能是数字、字母和下划线组成
{
alert("用户名只能是数字、字母和下划线组成,请重新输入");
form1.uname.value="";
form1.uname.focus();
return false;
}

但是在VBSCRIPT里面要怎么弄呢?能不能用这样的格式
<%uname=request.form("uname")
if uname(输入的不是数字,字母和下划线,就是这里的代码不知道格式) then
response.Write("错误类型:用户名含有非法字符")
%>
还有个end if笔误只是百度知道这里忘了加

VBS比较麻烦,需要创建正则对象来实现,以下是一个函数,你可以使用:
'************************************************
'** 判断字符串是否符合要求
'************************************************
Private Function ValidateString(strPatternIn,strContent)
Dim regEx, strTemp ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
Dim blnMatche
regEx.IgnoreCase = true ' 设置是否区分字符大小写。
regEx.Global = True ' 设置全局可用性。

regEx.Pattern = strPatternIn ' 设置模式。
blnMatche = regEx.test(strContent)

ValidateString = blnMatche

Set regEx = Nothing
End Function

A = "^\w+$" '// 正则表达式
B = "内容"
msgbox ValidateString(A,B)