-1

i am stuck in a problem...This is my code which restricts special characters but i want a logic which will restricts special characters,numerics but allow alphanumeric values... for eg:

  • valid : a1,4r,aa.
  • invalid : w@,12,@!.

    function check(e)

{

var keynum; var keychar; var numcheck; if(window.event) // IE { keynum = event.keyCode; } else if(e.which) // netscape/Firefox/opera { keynum = e.which; } //condition for backspace(8) Key if(keynum != 8) { keychar = String.fromCharCode(keynum); numcheck = /[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]*/; return numcheck.test(keychar); } else { return true; } 

}

 User id : <input type="text" id="txtname" name="txtname" onkeypress="return check(event)"/> 
6
  • A simple regular expression should be enough. Commented Jan 31, 2013 at 9:06
  • "Alphanumeric" means alphabetic + numeric characters, as explained in the wiki. As such, what do you mean by "restrict numerics", but "allow alphanumerics"? Commented Jan 31, 2013 at 9:06
  • Hartley... i want a1,4r,aa to be allowed but should not allow w@,12,@!. Commented Jan 31, 2013 at 9:07
  • check the link i have shared @chetanPotdar Commented Jan 31, 2013 at 9:11
  • So digits are okay if they are together with letters, but not a "word" containing only digits? Commented Jan 31, 2013 at 9:12

3 Answers 3

0

I believe this would satisfy your need.

[a-zA-Z0-9]*[a-zA-Z]+[a-zA-Z0-9]* 

This would restrict only numbers such as 12, 13, etc., would now allow any special character, and as required, would allow words containing alphabets and numbers both such as asd12, 12asd12, 12asd, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Alpha numeric validation from SO itself :

var reg_password1 = 'tes123'; var letters = /^[a-zA-Z0-9]+$/; var result = letters.test(reg_password1); alert(result); 

See this question

Fiddle not by me

1 Comment

it allows numeric only inputs also
0

I think this is the regex you need :

^[A-Za-z0-9]*[A-Za-z]+[A-Za-z0-9]*$ 

It matches zero or more alphanumeric characters followed by atleast one letter followed by zero or more alphanumeric characters.

So it allows aa and 0r

and does not allow 99 or sequence containing non-alphanumeric characters.

Your question says only alphanumeric characters are allowed. Your example says aa. should be allowed. If so, use :

^[A-Za-z0-9.]*[A-Za-z]+[A-Za-z0-9.]*$ 

It allows . along with at least one letter such as .aa and aa.

Check it like this :

numcheck.test(inputString); 

where inputString is the input like a23, we and .aa which are matched or 12 and k@ which are not matched

numcheck is /[A-Za-z0-9.]*[A-Za-z]+[A-Za-z0-9.]*/

12 Comments

am sorry, i wanted to update my answer but by mistake edited your answer, and do not know how to undo edit.
sorry but its not working...its not allowing me to enter thye numbers...eg.a1..
@chetanPotdar check well. It allows a1
@chetanPotdar do you want to allow special characters along with letters?
No naveed it is not allowing me...i have edited my whole code...plz have a luk on it..
|