Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I want a Javascript validation to force the user to start the username with a character .

Thanks in advance
Posted
Comments
Igor Kushnarev 24-Jan-11 2:04am    
Did you try regexp?
shakil0304003 24-Jan-11 5:31am    
what you tried?
Vinodh.B 24-Jan-11 5:36am    
this is wat i tried .

<script type="text/javascript">
function FirstLetterCharacter(val) {
if (val.value.length > 0) {
var firstLetter = val.value.substring(0, 1);
var remainingLetters = val.value.substring(1);
firstLetter = firstLetter.replace(/[^a-zA-Z]/, '');
if (firstLetter == '')
message.style.display = "block";
else
message.style.display = "none";
val.value = firstLetter + remainingLetters;
}
}
</script>

shakil0304003 24-Jan-11 5:40am    
You have to call the function in 'onKeyUp' event of textbox.
Vinodh.B 24-Jan-11 5:41am    
Yes . Am testing this script.

Steps:
1. Have a 'onKeyUp' event defined for the username textbox
2. In JS, based on the method defined for onKeyUp, check the letter pressed.
3. If the letter is not an alphabet then throw an error - show alert/message/text anything as desired.

OR

Above method was to throw an error on the fly. You can also do the validation at the time of create button click. At the button click, see if the username start from alphabet or not and act accordingly.

Try!
 
Share this answer
 
XML
<script type="text/javascript">
        function FirstLetterCharacter(val) {
            if (val.value.length > 0) {
                var firstLetter = val.value.substring(0, 1);
                var remainingLetters = val.value.substring(1);
                firstLetter = firstLetter.replace(/[^a-zA-Z]/, '');
                if (firstLetter == '')
                    message.style.display = "block";
                else
                    message.style.display = "none";
                val.value = firstLetter + remainingLetters;
            }
        }
    </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900