Results 1 to 7 of 7

Thread: [RESOLVED] ascii code [javascript]

  1. #1

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Resolved [RESOLVED] ascii code [javascript]

    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..

  2. #2
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ascii code [javascript]

    Check this: http://jalaj.net/2007/03/08/asc-and-chr-in-javascript/

    Example:
    html Code:
    1. <html>
    2. <body>
    3.  
    4. <script type="text/javascript">
    5.  
    6. var txt = "A";
    7. document.write(txt.charCodeAt(0));
    8.  
    9. </script>
    10.  
    11. </body>
    12. </html>
    It will return 65


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  3. #3

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: ascii code [javascript]

    Quote Originally Posted by akhileshbc View Post
    Check this: http://jalaj.net/2007/03/08/asc-and-chr-in-javascript/

    Example:
    html Code:
    1. <html>
    2. <body>
    3.  
    4. <script type="text/javascript">
    5.  
    6. var txt = "A";
    7. document.write(txt.charCodeAt(0));
    8.  
    9. </script>
    10.  
    11. </body>
    12. </html>
    It will return 65

    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>

  4. #4
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ascii code [javascript]

    Use
    Code:
    str.length-1
    Because the array starts from 0. So the upperbound would be 1 less than the length.

    If you still get any problems, let me know the input value you have given.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  5. #5

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: ascii code [javascript]

    no, it says valid for , . e.t.c
    i want to limit my characters from a to z and from 0 to 9.

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ascii code [javascript]

    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>
    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 !


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Hyperactive Member Aash's Avatar
    Join Date
    Dec 2009
    Location
    Earth
    Posts
    491

    Re: ascii code [javascript]

    yeah, u're right. I did it.
    Code:
    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;
    			}	
    		}		
    	
    }
    my stupid mistake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width