Click here to Skip to main content
15,116,654 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,
i want to write that a javascript function that allows only numbers,* and ?.
Posted
Comments
Maciej Los 16-May-13 2:52am
   
Have you tried something?

The regex is simple:
^[\d\*\?\.]*$
   
Comments
Mohibur Rashid 16-May-13 3:10am
   
i need to learn regex :)
OriginalGriff 16-May-13 3:23am
   
Get a copy of Expresso

http://www.ultrapico.com/Expresso.htm

Iit's free, and it examines and generates Regular expressions.
Mohibur Rashid 16-May-13 3:29am
   
thanks, sure will do
Arjun Menon U.K 20-May-13 1:58am
   
Hi, i have a doubt. i want to check a textbox value with regex that allow only a-z,A-Z,0-9.. i came across a regex ^[a-zA-Z0-9]+$ and ^[a-zA-Z0-9]*$
I would like to know what the +$ and *$ means. The ^ denotes starting of the string being checked rt? . Please correct if i am wrong
OriginalGriff 20-May-13 2:23am
   
+ means "one or more"
* means "zero or more"
^ means "start of string"
$ means "end of string"

Get a copy of Expresso - it explains this and helps a lot - the link is just above!
Arjun Menon U.K 20-May-13 4:14am
   
thanks griff
Arjun Menon U.K 20-May-13 4:16am
   
sadly its blocked by my company's firewall.. I want to check a value entered contains only numbers and characters from a-f. like hexadecial number,can i check like this ^[a-fA-F0-9]*$
OriginalGriff 20-May-13 4:38am
   
You might want to use a '+' instead of a '*', as that will match an empty field as well.
Arjun Menon U.K 20-May-13 4:18am
   
one more thing. If by giving the regEx exp as above does the test method check the order too. like whether the inputted value starts with characters then numbers.. ???
OriginalGriff 20-May-13 4:40am
   
No.
If you want characters-then-numbers you need a different regex such as:
^[A-Z]+[0-9]*$
Which expects one or more uppercase alpha followed by zero or more digits
There is several key event that can be handled for user input controlling.

for simplification I will only talk about onkeypress

to process a user input you must have to return true or false.

as example

HTML
<input type=text onkeypress='return false;'> <!-- this will discard whatever the user types-->
<input type=text onkeypress='return false;'> <!-- this will allow whatever the user types-->


try this on browser.

say you want to allow only 0 and 1. the simple example would be

HTML
<script>
function test(e){
 var e=window.event || e
 //alert(e.charCode);
 return e.charCode==48 || e.charCode==49 ? true:false;
}
</script>

<input type=text onkeypress='return test(event);'> <!--only let you print 0 or 1 -->


hope it will guide you a little
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900