Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Validation - No Capitals Allowed! 1

Status
Not open for further replies.

canajun

Programmer
Sep 28, 2002
57
CA
How can I produce an error message if a user uses capital letters in specific form input boxes? (username, password)

Thanks!
 
it would be far easier to change the values to lower case
example
<input type=&quot;text&quot; name=&quot;lowercaps&quot; onChange=&quot;javascript:this.value=this.value.toLowerCase();&quot;> Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Thanks, but I want the inputter to know they need to use lowercase letters.. Changing it my go unnoticed, and then cause further problems when they attempt to sign in.

Thanks....
 
try this
<html>
<head>
<SCRIPT>
function checkCase()
{
str = document.form1.lowercaps.value
// value to test
strLen = document.form1.lowercaps.value.length
// length for loop
for(i=0; i <= strLen-1; i++) {
if (str.charAt(i) < &quot;a&quot; || str.charAt(i) > &quot;z&quot;) {
// test for a character a to z
// for upper cahnge to A to Z
alert(&quot;Only lower case values are valid!&quot;);
return
}
}
}
</SCRIPT>
</head>
<body>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;lowercaps&quot; onBlur=&quot;return checkCase()&quot;>
</form>
</body>
</html> Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
after looing at that nasty loop I don't know why I did it that way. much easier to use a regex. try this
str = document.form1.lowercaps.value
var patrn = /^[a-z]+$/;
if (!(patrn.test(str))) {
alert(&quot;invalid!&quot;);
return false;
Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
OK.. now that we have that working.. how can we get the cursor to return (and highlight) the feild that has the error?

Thanks again... learning lots!
 
use the select() function to select the contents of the field and the focus() function to focus on it.
document.form1.lowercaps.select();
document.form1.lowercaps.focus(); Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top