Click to See Complete Forum and Search --> : alphabet or not check
satya
04-18-2003, 01:50 AM
iam displaying a text box for entering user name.
when the user clicks submit button it should check whether the user has entered any characters other than alphabets.if he do so it should prompt a message saying that "only alphabets are allowed".
Thanking u,
satya.
Nevermore
04-18-2003, 05:59 AM
Call this code with the submit button, and change the ID of your text box to one, or the line in the script to match the box:
<script type="text/javascript">
<!--
function check() {
element_one_value = document.getElementById("one").value;
element_one_value = element_one_value.toLowerCase();
seperate_values=element_one_value.split("");
for (x=0; x<seperate_values.length; x++) {
if (seperate_values[x] == "a" || seperate_values[x] == "b" || seperate_values[x] == "c" || seperate_values[x] == "d" || seperate_values[x] == "e" || seperate_values[x] == "f" || seperate_values[x] == "g" || seperate_values[x] == "h" || seperate_values[x] == "i" || seperate_values[x] == "j" || seperate_values[x] == "k" || seperate_values[x] == "l" || seperate_values[x] == "m" || seperate_values[x] == "n" || seperate_values[x] == "o" || seperate_values[x] == "p" || seperate_values[x] == "q" || seperate_values[x] == "r" || seperate_values[x] == "s" || seperate_values[x] == "t" || seperate_values[x] == "u" || seperate_values[x] == "v" || seperate_values[x] == "w" || seperate_values[x] == "x" || seperate_values[x] == "y" || seperate_values[x] == "z")
{continue;}
else {alert("Only standard alphabetic characters are valid."); return false; break;
}
}
}
//-->
</script>
It can be done with much less code using RegExp. Just switch the two items in bold to reflect the items (form and field name) of your page.
<script languag="javascript" type="text/javascript">
function check()
{
if (/[^a-z]/gi.test(document.myform.email.value))
{
alert ("Please use only standard alphabetic characters (a-z,A-Z)");
}
else
{
alert ("All's good");
}
}
</script>
hmmm, I have a period ( . ) in my name....:)
Originally posted by esm
hmmm, I have a period ( . ) in my name....:) He said it was for usernames. Maybe he doesn't want users to be able to use anything except standard alphabetic characters... ;)
jeffmott
04-19-2003, 12:34 AM
/[^a-z]/giWhy a global search? Once you find an invalid character to trigger the error what point is there in looking for another?