Page 1 of 1

Can anybody tell me about ....

Posted: Wed Jul 05, 2006 12:18 am
by eshban
can anybody tel methat how to resctrict the user to enter only alphabets in a form field.

if it is not possible in php then tell me javascript code

Posted: Wed Jul 05, 2006 1:06 am
by technofreak
Buddy,

You have to decide between PHP and Javascript. PHP does the processing on the server side, while JS does on the client side. This means, when a user types non-alphabetic charecters, PHP will find it only when they submit the whole set of data and it will be rejected by the PHP(in the server). Actually, this is a performance overhead. If you use JS, then you can detect the same as the user types the content and show up a warning to the user at the same instant. Thus, the load on the server/PHP to validate your form on the its end reduces, thereby improving the performance.

If you want to learn how do it in PHP, a simple reg-ex to find non alphabetical charecters can do the job. I am not aware of Javascripts, Please refer to a online tutorial which can teach you how to write simple javascripts like this. Search the mighty web for more details on reg-ex and javascripts. :) HTH !

Posted: Wed Jul 05, 2006 1:16 am
by RobertGonzalez
Yes, it is very possible and very easy. But you seriously need to a little work on your own before someone here does anything for you. Seach google or even these forums for 'alpha regex'. Then look up regular expression matching or something to that effect for code samples. After you try some things, if you still need help, post back and someone, I'm sure, will help you.

Posted: Wed Jul 05, 2006 1:23 am
by bdlang
Actually, you shouldn't decide between JS and PHP, you should use both. Validating on the client side is always a good idea as technofreak mentioned, but you need to do the same on the server side (the receiving PHP script) because the user may have JS turned off, for whatever reason. Or a malicious user may circumvent any client side checks you have. Heck, most form submissions on most sites can be completely faked and altered.

You can perform the same task using the same regular expression in both JS and PHP.

Posted: Wed Jul 05, 2006 6:25 am
by rameshmrgn
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

Try This....

Code: Select all

<html >
<head>
<script language="javascript" type="text/javascript">
function checkMe(txtBox)
	{
		if(!((event.keyCode >= 65 && event.keyCode<=90) || (event.keyCode >= 97 && event.keyCode<=122)))
			{
				event.keyCode=0;
			}	
	}
</script>
</head>
<body>
<input type="text" name="alpha" onkeypress="checkMe(this)" />
</body>
</html>

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jul 05, 2006 7:35 am
by bdlang
rameshmrgn wrote: Hi,

Try This....

Code: Select all

<html >
<head>
<script language="javascript" type="text/javascript">
function checkMe(txtBox)
	{
		if(!((event.keyCode >= 65 && event.keyCode<=90) || (event.keyCode >= 97 && event.keyCode<=122)))
			{
				event.keyCode=0;
			}	
	}
</script>
</head>
<body>
<input type="text" name="alpha" onkeypress="checkMe(this)" />
</body>
</html>
That's all find and well, but would you mind explaining exactly what that does to eshban?

Note:
Everah wrote: Yes, it is very possible and very easy. But you seriously need to a little work on your own before someone here does anything for you. Seach google or even these forums for 'alpha regex'. Then look up regular expression matching or something to that effect for code samples. After you try some things, if you still need help, post back and someone, I'm sure, will help you.