Results 1 to 10 of 10

Thread: VB/VB.Net/VBScript/JScript - Regular expressions to validate data ..

  1. #1

    Thread Starter
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456

    VB/VB.Net/VBScript/JScript - Regular expressions to validate data ..

    Regular Expressions to validate data against different data types ....

    1. ZIP_USA : "^\d{5}-\d{4}$
    2. ZIP_CANADA : ^[a-zA-Z][0-9][a-zA-Z][ ][0-9][a-zA-Z][0-9]$
    3. Phone : ^\(\d{3}\)-\d{3}-\d{4}$
    4. e-mail : ^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$
    5. Currency : ^\d*\.{0,1}\d+$
    6. Double : ^-{0,1}\d*\.{0,1}\d+$
    7. Single : ^-{0,1}\d*\.{0,1}\d+$
    8. Integer : ^-{0,1}\d+$
    Last edited by techyspecy; May 2nd, 2003 at 11:18 AM.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Can you give an example in any particular language? Javascript, or VB.NET maybe?

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by crptcblade
    Can you give an example in any particular language? Javascript, or VB.NET maybe?

    Here ya go buddy ...

    function replace(string, text, by) {
    // Replaces text with by in string

    var strLength = string.length, txtLength = text.length;

    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string; var newstr = string.substring(0,i) + by;
    if (i + txtLength < strLength)
    newstr += replace(string.substring(i + txtLength, strLength), text, by);
    return newstr;
    }

    function trimString(strText) {


    // this will get rid of leading spaces
    while (strText.substring(0,1) == ' ')
    strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces
    while (strText.substring(strText.length-1,strText.length) == ' ')
    strText = strText.substring(0, strText.length - 1);

    return strText;
    }

    function IsValidPhoneNumber(strPhone){
    return (strPhone.search(/^ *\+?([0-9])+(( |-)?[0-9])* *$/) != -1)
    };

    function FormatPhoneNumber(strPhone){
    return replace(trimString(strPhone),' ','-');
    }

    function IsValidEmail( strEmail ){
    return (strEmail.search(/^ *\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+ *$/) != -1)
    };

    function FormatEmail(strEmail){
    return trimString(strEmail);
    };

    function IsValidNumber(strNumber){
    return (parseFloat(strNumber) != strNumber);

    }

    function IsEmpty(str){
    return (str.search(/^ *$/) != -1)
    };

    If you need something specific let me know ....

  4. #4

  5. #5

    Thread Starter
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Originally posted by MartinLiss
    Can you give a VB example?
    Here ya go Marty -

    Just add a reference to Microsoft VBScript Regular Expressions 1.0 or whatever version you have on your machine ... and Create a empty form with a command button and textbox on it. Type any email in textbox and click the button ....

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. Dim l_objRegExp As New RegExp
    5. Dim l_objPatternResult As Object
    6.  
    7.  
    8.     l_objRegExp.Pattern = "^ *\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+ *$"
    9.     Set l_objPatternResult = l_objRegExp.Execute(Text1.Text)
    10.    
    11.     If l_objPatternResult.Count > 0 Then
    12.         MsgBox "Valid Email ID"
    13.     Else
    14.         MsgBox "Invalid Email ID"
    15.     End If
    16. End Sub

  6. #6

    Thread Starter
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    I do not know if you guys are already familiar with them or not, but if you are willing to explore them more in greater detail here is the link .....

    http://msdn.microsoft.com/library/de...xpressions.asp

    I think they are pretty slick, its a great and easy way to validate different set of data, all you have to do is define a pattern of validation for them. They are specially helpfull in web development.

  7. #7
    New Member Caresse's Avatar
    Join Date
    Jun 2004
    Location
    Singapore
    Posts
    12
    Hi,

    I saw this entry of validation and i find it very useful. I tried testing the email validation using visual basic. But when i compile and run it, it gives me an error of 'User-defined type not defined'User-defined type not defined' for the
    VB Code:
    1. Dim l_objRegExp As New RegExp

    I can't find the expression for RegExp . Is it possible for you to advise me on this? I am currently using Visual Basic 6.0. Hope to hear a reply from you soon. Thank you.

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Look for a reference along the lines of "Microsoft VBScript Regular Expressions"
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  9. #9

  10. #10
    New Member Caresse's Avatar
    Join Date
    Jun 2004
    Location
    Singapore
    Posts
    12
    Oops sorry.. Didn't notice that. Thank you guys for your help. Now my code works perfectly. Thanks again.

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