hongyi
09-09-2002, 07:34 AM
how do i check a string that user entered is minimum 9 characters?
and if the user enters less than 9 characters, prompt an error message.
pls help asap...
fivesidecube
09-09-2002, 12:47 PM
hongyi,
Try the following code. Changing the TestString scalar will demonstrate what happens when the string is too short.
$TestString = "HelloHello";
if( length $TestString < 9 )
{
print "The string is too short\n";
}
else
{
print "The length of the string is OK\n";
}
hongyi
09-10-2002, 02:58 AM
what if i am checking for all these fields also?
$NRICNo_1stchar = substr($NRICNo, 0, 1);
$NRICNo_9thchar = substr($NRICNo, 8, 1);
$NRICNo_middlechar = substr($NRICNo, 1, 7);
if ($NRICNo_1stchar =~ /[^FS]/) {
@return_array = ("false","NRICNo First character must be a uppercase \"S\" or \"F\" ");
return (@return_array);
}
elsif ($NRICNo_9thchar =~ /[^A-Z]/) {
@return_array = ("false","NRICNo last character must be a uppercase letter");
return (@return_array);
}
elsif ($NRICNo_middlechar =~ /\D/) {
@return_array = ("false","NRICNo middle 7 characters must be digits");
return (@return_array);
}
then i do i know if the NRICNo_1stchar + NRICNo_middlechar + $NRICNo_9thchar = NRICNo_totalchar which is in total 9 chars...
fivesidecube
09-10-2002, 09:00 AM
hongyi,
You can always check that the length is correct by joining the different scalars together:
if( length( NRICNo_1stchar . NRICNo_middlechar . $NRICNo_9thchar) == 9 )
{
print "Correct length\n";
}
else
{
print "Incorrect length\n";
}
If you are needing the different fields from the $NRICNo scalar, you can always use a regular expression to select them:
$TestString = "F1111111D";
$_=$TestString;
if( /^([FS])(\d\d\d\d\d\d\d)([A-Z])$/ )
{
print "Matched OK\n\n";
print "For the String $TestString\n";
print "First part: $1\n";
print "Second part: $2\n";
print "Third part: $3\n\n";
}
else
{
print "Matched failed\n";
print "TestString needs to be of the format [F|S]nnnnnnn[A-Z]\nWhere:\n\tn is a digit\n\n";
}
Hope this helps.