Results 1 to 6 of 6

Thread: How to validate a text field with a custom validation with a Regular Expression?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70

    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>

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Why not use the RegularExpressionValidator control for this?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    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?

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    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?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2004
    Location
    Barcelona
    Posts
    70
    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
  •  



Click Here to Expand Forum to Full Width