hi i have a form ...i want to validate the fields in such a way it does not accept if the user is trying to enter numeric instead of characters..........
eg in a name field if the user is trying to enter number....the typed data should not come...it should act like readonly field..........

Recommended Answers

All 8 Replies

Hi.

I use this function to validate name in my forms :

function valName($name) 

{
      $name = preg_replace(‘/[\s]+/is’, ‘ ‘, $name);
      $name = trim($name);

      return preg_match(‘/^[a-z\s]+$/i’, $name);            
}

I use javascript for any form validation that I require and resuse where required.

Javascript:
// Validates Numbers 0-9 0=48
function Numbers(e) {
var cc = (e.which) ? e.which : event.keyCode;
if (cc > 47 && cc < 58) {
return true;
}
return false;
}


Field:
onKeyPress="Numbers()"

Hope this helps, Regards X :)

thanks for your reply
did you mean something like this............

<html>
<head>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
function Numbers(e) {
var cc = (e.which) ? e.which : event.keyCode;
if (cc > 47 && cc < 58) {
return true;
}
return false;
}
</SCRIPT>
</head>
<body>
<form method="">
<input type="text" name="jtitle" onKeyPress="Numbers()"
size="57">
</body>
</html>
i tried like this but it is not working.......

I would validate on both client side and server side(because sometimes, the user might have disabled the javascript!)

sorry my fault, forgot this(change the line to):

<input type="text" name="jtitle" onKeyPress="return Numbers(this)"
size="57">

It will work 100% worked for me, enjoy
sorry for the mistake :(

sorry my fault, forgot this(change the line to):

<input type="text" name="jtitle" onKeyPress="return Numbers(this)"
size="57">

It will work 100% worked for me, enjoy
sorry for the mistake :(

I dont mean to say that your method doesnt work, but if you use this javascript function, the user can still copy and paste letters into the box. I havent tested MitkOK's code, but i think that would work better, since it would eliminate the chance of copy + paste.

Is it?
I havent tested copy n paste.
Well lets add it I guess, anyone know how to stop it in javascript?
Ill research when im feeling better (busted me hand) =(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.