Script works except for one thing - I need to flag a string as invalid if it has
ID: 3636282 • Letter: S
Question
Script works except for one thing - I need to flag a string as invalid if it has 2 uppercase letters and one or more lowercase ltetters - here's what I have so far:ad>
<title>ssn Checker</title>
<script type="text/javascript">
var RE_SSN = /[A-Z][a-z]2/;
function tst_name(ssn){
if (RE_SSN.test(ssn)) {
alert("VALID name");
} else {
alert("INVALID name");
}
}
</script>
</head>
<body>
<form>
<input type="text" name="ssn" size="20">
<input type="button" value="This returns an invalid if string is all uppercasewhich has to have the 1st letter be uppercase and the rest lowercase"
>
</form>
</body>
</html>
Explanation / Answer
Hi,
A couple of notes:
1. Give this a try as your regular expression:
var RE_SSN = /^[A-Z]{1}[a-z]+$/;
This says:
[A-Z]{1} -> Exactly one capital letter...
[a-z]+ -> ...followed by 1 or more lowercase letters
The surrounding ^ and $ charcters say treat the entire string as one match - this ensures that multiple 'valid' expressions won't be treated as valid:
'Abcdef' is valid, but 'AbcDef' is not
2. You also need to use the .match() method when testing regular expressions, not .test().
if (ssn.match(RE_SSN)) ...
3. You need the Regular Expression to be inside the .match() call (not the value you're testing).
I'm hoping this is what you were looking for.
Give it a shot and let me know if you have any questions.
Thanks,
Joe
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.