以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  以A开头的14位编码的验证  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=124249)

--  作者:susu312
--  发布时间:2018/9/3 10:32:00
--  以A开头的14位编码的验证

需求:机构编码是以A开头的14位数

 

validating:

 

Dim pattern As String =  "^A[0-9]{13}$"
Dim txt As String = e.Sender.Text
If txt > "" Then
    Dim rgx = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
    If rgx.isMatch(txt) = False Then
        e.Sender.Error = "机构编码是以A开头的14位数!"
        e.cancel = True
    Else
        e.Sender.Error = ""
    End If
Else
    e.Sender.Error = ""
End If

 

keydown:

If e.KeyCode =Asc("A") OrElse e.KeyCode >= Asc("0") AndAlso e.keycode <= Asc("9") Then
    If e.sender.text.length > 13 Then
        e.cancel = True
    End If

ElseIf e.KeyCode <> Keys.back Then
    e.cancel = True

End If

 

老师,这个小写a也能输进去,还有就是应该只允许输入A和数字,,请老师帮忙改一下


--  作者:有点甜
--  发布时间:2018/9/3 10:56:00
--  
Dim pattern As String =  "^A[0-9]{13}$"
Dim txt As String = e.Sender.Text
If txt > "" Then
    Dim rgx = new System.Text.RegularExpressions.Regex(pattern)
    If rgx.isMatch(txt) = False Then
        e.Sender.Error = "机构编码是以A开头的14位数!"
        e.cancel = True
    Else
        e.Sender.Error = ""
    End If
Else
    e.Sender.Error = ""
End If

--  作者:susu312
--  发布时间:2018/9/3 11:07:00
--  
以下是引用有点甜在2018/9/3 10:56:00的发言:
Dim pattern As String =  "^A[0-9]{13}$"
Dim txt As String = e.Sender.Text
If txt > "" Then
    Dim rgx = new System.Text.RegularExpressions.Regex(pattern)
    If rgx.isMatch(txt) = False Then
        e.Sender.Error = "机构编码是以A开头的14位数!"
        e.cancel = True
    Else
        e.Sender.Error = ""
    End If
Else
    e.Sender.Error = ""
End If

谢谢老师,

我发现我的

keydown只限制了长度,汉字啊,别的字母也能输入进行,应该是让只能输入A和数字

 

 

If e.KeyCode =Asc("A") OrElse e.KeyCode >= Asc("0") AndAlso e.keycode <= Asc("9") Then
    If e.sender.text.length > 13 Then
        e.cancel = True
    End If

ElseIf e.KeyCode <> Keys.back Then
    e.cancel = True

End If


--  作者:有点甜
--  发布时间:2018/9/3 11:37:00
--  

keydown无法限制中文输入法的(搜狗拼音、五笔等等)

 

1、你可以设置掩码 http://www.foxtable.com/webhelp/scr/3100.htm

 

2、你可以在enter事件切换到英文输入法

 

For Each lang As Object In Windows.Forms.InputLanguage.InstalledInputLanguages
    \'msgbox(lang.LayoutName)
    If lang.LayoutName Like "*搜狗拼音输入法*" Then
        Windows.Forms.InputLanguage.CurrentInputLanguage = lang

        \'msgbox("切换到搜狗输入法成功")
        Exit For
    End If
Next

 

3、你可以在textchanged事件写判断的代码,判断是否输入正确

 


--  作者:susu312
--  发布时间:2018/9/3 11:58:00
--  
以下是引用有点甜在2018/9/3 11:37:00的发言:

keydown无法限制中文输入法的(搜狗拼音、五笔等等)

 

1、你可以设置掩码 http://www.foxtable.com/webhelp/scr/3100.htm

 

2、你可以在enter事件切换到英文输入法

 

For Each lang As Object In Windows.Forms.InputLanguage.InstalledInputLanguages
    \'msgbox(lang.LayoutName)
    If lang.LayoutName Like "*搜狗拼音输入法*" Then
        Windows.Forms.InputLanguage.CurrentInputLanguage = lang

        \'msgbox("切换到搜狗输入法成功")
        Exit For
    End If
Next

 

3、你可以在textchanged事件写判断的代码,判断是否输入正确

 

 

1、我用掩码,结果它跳行了,第三个框只让输入14位,超过后它就跳到第二行第一行了,

 

3、textchanged事件代码:

 

Dim txt As String = e.Sender.Text
If txt > "" Then
    Dim pattern As String = "[^A\\d+]"
    Dim str As String = System.Text.RegularExpressions.Regex.Replace(txt , pattern ,"")
    e.Sender.Text = str
    e.sender.SelectionStart = str.Length
End If

 

这个现在输入数字没问题 ,但是只允许输入数字,如何还让它只允许输入A和数字?

 

 

 


此主题相关图片如下:111.png
按此在新窗口浏览图片

 

还是跳行问题?请老师指导下这个该如何解决?

[此贴子已经被作者于2018/9/3 11:59:57编辑过]

--  作者:有点甜
--  发布时间:2018/9/3 12:21:00
--  

1、

 

Dim txt As String = e.Sender.Text
If txt > "" Then
    Dim pattern As String = "[^A\\d]"
    Dim str As String = System.Text.RegularExpressions.Regex.Replace(txt , pattern ,"")
    e.Sender.Text = str
    e.sender.SelectionStart = str.Length
End If

 

2、设置掩码就是会自动跳行的