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

Dani AI

Generated

Short answer: combine client-side sanitizing (to stop typing and pasted digits) with a server-side check (to never trust the browser). This keeps the UI responsive (no visible digits ever) and preserves security when JavaScript is disabled or bypassed.

's keypress idea is fine for basic blocking, and is right that paste can sneak bad input through. Use the input event to sanitize the value (it covers typing, IME, mobile keyboards and paste when paired with a paste handler). Example JavaScript that strips anything that is not a letter, combining caret preservation so user experience stays natural:

function filterNameInput(el) {
  var start = el.selectionStart;
  var before = el.value;
  // allow letters (Unicode), marks, spaces, apostrophe and hyphen
  var cleaned = before.replace(/[^\p{L}\p{M}'\-\s]/gu, '');
  if (cleaned !== before) {
    el.value = cleaned;
    var pos = Math.max(0, start - (before.length - cleaned.length));
    el.setSelectionRange(pos, pos);
  }
}
document.getElementById('name').addEventListener('input', function(){ filterNameInput(this); });
document.getElementById('name').addEventListener('paste', function(){ var el=this; setTimeout(function(){ filterNameInput(el); },0); });

Always validate again on the server. Building on 's server-side idea, use a Unicode-aware pattern so accented names work:

$name = trim($name);
if (!preg_match('/^[\p{L}\p{M}\'\-\s]+$/u', $name)) {
    // reject or normalize input
}

Notes and gotchas: allow hyphen and apostrophe for names like O'Neill or Anne-Marie; be careful if your app must accept numeric suffixes (e.g., "John II") — decide policy and document it. Test with copy/paste, mobile keyboards, and IMEs. If older browsers lack Unicode property escapes, fall back to a simpler ASCII pattern or use an HTML5 pattern attribute as a progressive enhancement, but never skip the server check as advised.

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.