Wednesday, May 20, 2015

Powerbuilder Function to Check Email Address Validity

This function was made based on Visual Basic scripting. The original was created by Chad M. Kovac, and I tried to translate into Powerbuilder language and fix some minor bugs.




1:  Boolean bOK = True  
2:  String strDomainType  
3:  String strDomainName  
4:  Integer i  
5:    
6:  Constant String sInvalidChars = "!#$%^&*()=+{}[]|\;:'/?>,< "  
7:    
8:  // if there double quote character  
9:  If Pos(sEmail, Char(34)) > 0 Then bOk = False  
10:  If Left(sEmail,1) = "." Or Right(sEmail,1) = "." Then bOK = False  
11:    
12:  If bOK Then  
13:     // Check for invalid characters.  
14:     If Len(sEmail) > Len(sInvalidChars) Then  
15:         For i = 1 To Len(sInvalidChars)  
16:            If Pos(sEmail, Mid(sInvalidChars, i, 1)) > 0 Then  
17:                 bOK = False  
18:                 Exit  
19:            End If  
20:         Next  
21:     Else  
22:         For i = 1 To Len(sEmail)  
23:            If Pos(sInvalidChars, Mid(sEmail, i, 1)) > 0 Then  
24:                 bOK = False  
25:                 Exit  
26:            End If  
27:         Next  
28:     End If  
29:       
30:     If bOK Then  
31:        //Check for an @ symbol  
32:        If Pos(sEmail, "@") > 1 Then   
33:            bOK = f_if(Len(Left(sEmail, Pos(sEmail, "@", 1) - 1)) > 0,True,False)  
34:        Else  
35:            bOk = False  
36:        End If  
37:    
38:        If bOK Then  
39:           //Check to see if there are too many @'s  
40:           sEmail = Right(sEmail, Len(sEmail) - Pos(sEmail, "@"))  
41:           bOK = f_if(Pos(sEmail, "@") > 0, False, True)   
42:    
43:           If bOK Then  
44:              strDomainType = Right(sEmail, Len(sEmail) - Pos(sEmail, "."))  
45:              bOK = f_if(Len(strDomainType) > 0 And Pos(sEmail, ".") < Len(sEmail), True, False)  
46:    
47:              If bOK Then  
48:                 sEmail = Left(sEmail, Len(sEmail) - Len(strDomainType) - 1)  
49:                 Do Until Pos(sEmail, ".") <= 1  
50:                     If Len(sEmail) >= Pos(sEmail, ".") Then  
51:                        sEmail = Left(sEmail, Len(sEmail) - (Pos(sEmail, ".") - 1))  
52:                     Else  
53:                        bOK = False  
54:                        Exit  
55:                     End If  
56:                 Loop  
57:                 If bOK Then  
58:                    If sEmail = "." Or Len(sEmail) = 0 Then bOK = False  
59:                 End If  
60:              End If  
61:           End If  
62:        End If  
63:     End If  
64:  End If  
65:    
66:  Return bOk  

Here's the example to use this function

1:  if f_isvalidemail('youremailaddress@domain.com') then  
2:     messagebox('','Valid')  
3:  else  
4:     messagebox('','Invalid')  
5:  end if  

No comments:

Post a Comment