Page 1 of 1

Validation

Posted: Mon Oct 12, 2009 2:56 pm
by bhowden
As part of an exercise for an assignment I need to know how to perform validation in Jade. I have a set method which returns a string called LastName and one called FirstName. At the moment this method just returns true I need to know how to change this so that it checks for special characters such as apostrophoses and hyphens and if the string contains any of these return false other wise if all is ok return true.

Re: Validation

Posted: Tue Oct 13, 2009 7:43 am
by BeeJay
You would need to code your setDetails method along the lines of the followig:

Code: Select all

setDetails( pFirstName : String ; pLastName : String ) : Boolean ; begin if pFirstName.containsSpecialCharacters or pLastName.containsSpecialCharacters then // Invalid first or last name, so bail now return false ; endif; // The names validate ok, so set the properties and return true to indicate success self.firstName := pFirstName ; self.lastName := pLastName ; return true ; end;
Obviously you then need to create the String::containsSpecialCharacters primitive method to return true/false dependent on whether or not that string contains any of what you consider to be special characters. Given that this is for a programming assignment, I won't write example code for that method. You should, however, review the RootSchema supplied methods on the String primitive as there are existing methods that could help achieve the desired result. In particular, the String::scan**** methods could prove useful in writing an efficient String::containsSpecialCharacters method to suit your requirements.

Cheers,
BeeJay.