How to: Validate special characters
Overriding the method ValidateField() from table.
This is how it works on ValidateField():
This is my class:
This is how it works on ValidateField():
public boolean validateField(FieldId _fieldIdToCheck)
{
boolean ret;
ret = super(_fieldIdToCheck);
if(ret)
{
switch(_fieldIdToCheck)
{
case fieldNum (LogisticsEletronicAddress, Locator):
ret = ValidateCharacter::validateCharacter(this.Locator) ;
if(!ret)
error( "The eletronic address has invalid characters" );
break;
}
}
return ret;
}
For example, I have used the Table LogisticsEletronicAddress which is responsible to save contact information like e-mail and telephone. I will check if I am validating the field Locator and then call my class. Suppose I have typed “264-497=3131″.This is my class:
public static boolean validateCharacter(str _name)
{
System.Text.RegularExpressions.Match regExMatch;
boolean isValid = true;
int length;
int counter;
container specialCharacters;
str value;
;
// Here I insert all the special characters I want to validate.
specialCharacters = conIns(specialCharacters,1, "/");
specialCharacters = conIns(specialCharacters,2, "[");
specialCharacters = conIns(specialCharacters,3, "]");
specialCharacters = conIns(specialCharacters,4, ";");
specialCharacters = conIns(specialCharacters,5, "=");
specialCharacters = conIns(specialCharacters,6, @'\');
specialCharacters = conIns(specialCharacters,7, ":");
specialCharacters = conIns(specialCharacters,8, ",");
specialCharacters = conIns(specialCharacters,9, ".");
specialCharacters = conIns(specialCharacters,10, "!");
specialCharacters = conIns(specialCharacters,11, "-");
specialCharacters = conIns(specialCharacters,12, "(");
specialCharacters = conIns(specialCharacters,13, ")");
specialCharacters = conIns(specialCharacters,14, "*");
specialCharacters = conIns(specialCharacters,15, "&");
specialCharacters = conIns(specialCharacters,16, "^");
specialCharacters = conIns(specialCharacters,17, "#");
specialCharacters = conIns(specialCharacters,18, "@");
specialCharacters = conIns(specialCharacters,19, "_");
specialCharacters = conIns(specialCharacters,20, "$");
specialCharacters = conIns(specialCharacters,21, "%");
specialCharacters = conIns(specialCharacters,22, "?");
specialCharacters = conIns(specialCharacters,23, "+");
specialCharacters = conIns(specialCharacters,24, "{");
specialCharacters = conIns(specialCharacters,25, "}");
specialCharacters = conIns(specialCharacters,26, "<");
specialCharacters = conIns(specialCharacters,27, ">");
specialCharacters = conIns(specialCharacters,29, "~");
// I set my container length size.
length = conLen(specialCharacters);
// Looping through the container
for (counter = 1; counter <= lenght; counter++)
{
// Getting the container value.
value = conPeek(specialCharacters, counter);
// Here I use the method StrContains(), it will check the String
// character-by-character and return true if it matches the desired value.
if(strContains(_name, value))
{
isValid = false;
}
}
// Return validation result.
return isValid;
}
I have used all the special characters that I know of but feel free to add as much as your client requires :). This code will check the string “264-497=3131″ character-by-character and match all the 28 special characters and return it as false (Let’s forget that my telephone number accepts alphabetical characters haha).
No comments:
Post a Comment