hi,
I want to know how to get ascii code for a letter, can anyone will tell me?
i searched a lot but couldn't find the javaScript functions..
thanks..
Printable View
hi,
I want to know how to get ascii code for a letter, can anyone will tell me?
i searched a lot but couldn't find the javaScript functions..
thanks..
Check this: http://jalaj.net/2007/03/08/asc-and-chr-in-javascript/
Example:
It will return 65html Code:
<html> <body> <script type="text/javascript"> var txt = "A"; document.write(txt.charCodeAt(0)); </script> </body> </html>
:wave:
thanks :)
i had seen that web page before.
i'm reading character by character and want to check. Can u tell me where i'm wrong?
Code:<html>
<head>
<title>verify</title>
<script>
function check()
{
var str=document.my.first.value;
for(i=0; i<=str.length; i++)
{
if((str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122))
{
alert("valid");
break;
}
else
{
alert("Invalid");
break;
}
}
}
</script>
</head>
<body>
<form name=my id="my">
<table>
<tr>
<td><input type='text' name='first'></td><td></td>
</tr>
<tr>
<td><input type='button' name='click' value='check ' onClick='check()'></td>
</tr>
</table>
</body>
</html>
Use
Because the array starts from 0. So the upperbound would be 1 less than the length.Code:str.length-1
If you still get any problems, let me know the input value you have given.
:wave:
no, it says valid for , . e.t.c
i want to limit my characters from a to z and from 0 to 9.
Also, you have to change statements in TRUE part of IF condition. What you have done is, you'll start checking each character. If it is valid, you will display a message and breaks the execution of the FOR loop. But that will prevent the code from checking other characters !Code:<html>
<head>
<title>verify</title>
<script>
function check()
{
var str=document.my.first.value;
var f=0;
for(i=0; i<=str.length-1; i++)
{
if((str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122))
{
f=0;
}
else
{
f=1;
break;
}
}
if(f==1)
alert("Invalid");
else
alert("Valid");
}
</script>
</head>
<body>
<form name=my id="my">
<table>
<tr>
<td><input type='text' name='first'></td><td></td>
</tr>
<tr>
<td><input type='button' name='click' value='check ' onClick='check()'></td>
</tr>
</table>
</body>
</html>
:wave:
yeah, u're right. I did it.
my stupid mistake :DCode:function check()
{
var str=document.my.first.value;
for(i=0; i<=str.length-1; i++)
{
if((str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122))
{
}
else
{
alert("Invalid");
break;
}
}
}