View Full Version : Input Validation - Breaking up a string
redwood_player
01-12-2007, 06:23 AM
Hi All,
Does anyone know how I could take apart a word and double check that each letter contains only 1 of four characters? For example, if I wanted a character sequence to contain only the characters a thru d, and the user inputted the sequence...
adda
what would the code be?
Right now Im thinking it has to be a for loop that continually advances the character position, checking each with an if statement each time.
if (firstChar == (A || B || C || D))
System.out.println("Contains one of the right characters");
------Some type of command to move on to the second character-----
if (secondChar == (A || B || C || D))
System.out.println("contains one of the right characters");
and etcetera.
Please advise.
Regards,
redwood player
whizard
01-12-2007, 06:52 AM
This should do it in PHP (though I haven't actually tested it)
<?php
$string = //WORD YOU ARE CHECKING
$good_chars = array("A","B","C","D");
$letters = str_split($string);
$i = 0;
while($i < strlen($string))
{
if(!in_array($string[$i],$good_chars))
{
die("Error: Letter #".$i+1." was invalid.");
}
else
{
print("Contains one of the right characters.");
}
$i++
}
?>
I know, there's probably a great Regex way to do that, but Regex scares me...
HTH
Dan
redwood_player
01-12-2007, 07:02 AM
I have to put in in java code. This is what I've come up with so far. How can I include the rest of the letters?
if (pwdLength == inputLength)
{int i = 0;
testChar = pwdInput.substring(i, i+1);
if (testChar.equalsIgnoreCase("P")) // How do I add in the rest of the letters?
{System.out.println("Character at position " + i + "validated");
i++;}
else
{System.out.println("Your character sequence includes something other than P, U, L, and N\n" +
"Please input the sequence again.\n");
System.out.print("Character Sequence: ");
pwdInput = keyboard.nextLine();
inputLength = pwdInput.length();}
}
else
{System.out.println("Your character sequence has either too many or too few characters\n" +
"Please input the sequence again.\n");
System.out.print("Character Sequence: ");
pwdInput = keyboard.nextLine();
inputLength = pwdInput.length();}
whizard
01-12-2007, 07:05 AM
Sorry, I don't know any java...
Doesn't java have arrays?
Dan
oracleguy
01-12-2007, 07:19 AM
You can use the following Reg Ex (Regular Expression) to test to see if it meets your specifications:
^([a-d])+$
That will match if the word contains only the characters a, b, c, d and if it is of zero length, it will not pass either.
I don't know how to evaluate regex's in Java but from the other languages I've used them in, when you are just comparing a string against a pattern, its typically pretty easy.
rpgfan3233
01-12-2007, 05:07 PM
If you want to allow both uppercase and lowercase letters, "[A-Da-d]+" should do it. What that does is allows all uppercase and lowercase letters A, B, C and D to be allowed, provided that the string is not a zero-length string.
oracleguy
01-12-2007, 07:37 PM
If you want to allow both uppercase and lowercase letters, "[A-Da-d]+" should do it. What that does is allows all uppercase and lowercase letters A, B, C and D to be allowed, provided that the string is not a zero-length string.
Well actually it could register some false positives in this use unless you make the pattern encompass the entire string with the ^ and $: ^[A-Da-d]+$
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.