Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    check a string for number of characters

    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...

  • #2
    New Coder
    Join Date
    Sep 2002
    Location
    Up North (UK)
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hongyi,
    Try the following code. Changing the TestString scalar will demonstrate what happens when the string is too short.
    Code:
    $TestString = "HelloHello";
    
    if( length $TestString < 9 )
    {
    	print "The string is too short\n";
    }
    else
    {
    	print "The length of the string is OK\n";
    }

  • #3
    New Coder
    Join Date
    Aug 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #4
    New Coder
    Join Date
    Sep 2002
    Location
    Up North (UK)
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hongyi,

    You can always check that the length is correct by joining the different scalars together:
    Code:
    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:
    Code:
    $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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •