|
-
May 1st, 2003, 11:51 AM
#1
Thread Starter
Let me in ..
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.
-
May 1st, 2003, 12:26 PM
#2
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
-
May 2nd, 2003, 07:25 AM
#3
Thread Starter
Let me in ..
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 ....
-
May 3rd, 2003, 10:54 AM
#4
Can you give a VB example?
-
May 3rd, 2003, 01:45 PM
#5
Thread Starter
Let me in ..
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:
Option Explicit
Private Sub Command1_Click()
Dim l_objRegExp As New RegExp
Dim l_objPatternResult As Object
l_objRegExp.Pattern = "^ *\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+ *$"
Set l_objPatternResult = l_objRegExp.Execute(Text1.Text)
If l_objPatternResult.Count > 0 Then
MsgBox "Valid Email ID"
Else
MsgBox "Invalid Email ID"
End If
End Sub
-
May 3rd, 2003, 01:50 PM
#6
Thread Starter
Let me in ..
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.
-
Jun 24th, 2004, 03:42 AM
#7
New Member
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:
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.
-
Jun 24th, 2004, 10:45 AM
#8
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
-
Jun 24th, 2004, 10:46 AM
#9
Originally posted by techyspecy
...Just add a reference to Microsoft VBScript Regular Expressions 1.0 or whatever version you have on your machine ...
-
Jun 24th, 2004, 08:08 PM
#10
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|