|
-
Mar 9th, 2004, 10:28 AM
#1
Thread Starter
Lively Member
How to validate a text field with a custom validation with a Regular Expression?
Hi !
I have a custom validation with a regular expression to check if the text typed in a text box is correct. But I don' t know how to check if it' s correct the text typed with the regular expression. For instance, according to my RegExp, if somebody type 'B2583691' is correct (1 letter and 7 numbers), but 'A123456' isn' t correct (1 letter and 6 numbers).
Now I do it with the 'match' class, but I think that isn' t correct for my purpose.
Nobody knows how can I do this kind of validation?
My Sub:
Code:
Sub ServerVal_id(sender As Object, value As ServerValidateEventArgs)
Try
Dim kindId As Integer
kindId = var_id.SelectedItem.Value
Dim id As String
id = idNumber.Text()
Dim patNum As String = "^[ABCDEFGHKLMNPQS]{1}[0-9]{7}$"
Dim n As New Regex(patNum)
If (kindId = 1) Then
Dim checkNum As Match = n.Match(id)
If checkNum.Success Then
value.IsValid = True
Exit Sub
Else
value.IsValid = False
End If
...
End If
Catch exc As Exception
End Try
End Sub
Then in the control:
Code:
<asp:CustomValidator id="CustomValidator2" runat="server" ControlToValidate="idNumber" OnServerValidate="ServerVal_id" Display="Dynamic">Incorrect format for kind 1.
</asp:CustomValidator>
-
Mar 9th, 2004, 11:55 AM
#2
PowerPoster
Why not use the RegularExpressionValidator control for this?
-
Mar 9th, 2004, 12:20 PM
#3
Thread Starter
Lively Member
Because the validation depends on the selection in a 'dropdownlist'. If the user selects 1 he/she needs to type a pattern into a text box, if user selects 2 she/he needs to type a different pattern. The error message will be different in both cases.
So it's a little bit more complex. Isn' t it?
-
Mar 9th, 2004, 08:57 PM
#4
I wonder how many charact
I'm guessing what you're asking for is the RegEx to help you validate the expression.
Other than the expression consisting of one letter and seven numbers, the only other requirement I can infer is that the first character must be a letter (between A-S, minus I,O,R), and the match must end in 7 consecutive digits.
Correct?
Javascript regex:
/^[(A|B|C|D|E|F|G|H|J|K|L|M|N|P|Q|S){1}]\d{7}$/
Last edited by nemaroller; Mar 9th, 2004 at 09:18 PM.
-
Mar 10th, 2004, 04:50 AM
#5
Thread Starter
Lively Member
I know the meaning of my RegExp. I suppose that the meaning is the same with 'javascript' than in 'asp.net vb.net'... Because I have copied it from a javascript function.
Has the RegExp the same language-meaning in javascript than in asp.net vb.net?
-
Mar 10th, 2004, 12:13 PM
#6
Thread Starter
Lively Member
Now my Subroutine runs, I have 'found' the problem, but it is something very strange and I need to solve it . If I erase all the server control validations from the page except the custom validation, the custom validation runs. It is really very strange...
Any ideas?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|