I have been trying to research this in my spare time for the last few weeks now and everything turns a blank.

Yes I can restrict input in IE but in FF I cant.

I am looking to restrict input based on alpha, numeric and special characters only - in seperate boxes.

So anyone have any information that could help?

Thanks, Regards X

Note: I am looking for something that works in BOTH IE and FF but before posting can you please make sure it works and if it only works in IE or FF please state it, thanks.

I can help u out with the numeric one, if u search for regular expressions to restrict alpha and special characters, u should find something that works in both.

function checkNumeric(entry)
    {
        var numeric = /^[0-9]*$/; 
        if (!numeric.test(entry.value)) 
            {
               document.getElementById('errorText').style.display = '';
            }else{
                    document.getElementById('errorText').style.display = 'none';
            }
        entry.value = entry.value.replace(/[^0-9]/g,"");
    }

I tried to use the above example to restrict input into the text field with no avial.

Maybe you can show me the html you used with that function to get it working.

Thanks, Regards X

sure here it is:

<table>
<tr>
        <td>Display Limit:</td>
        <td><input type="text" maxlength="5" style="width:95px;" name="displayLimit" onkeyup="checkNumeric(this)" id="displayLimit" value="100" /><span id="errorText" style="padding-left:5px; display:none;" class='errorTextClass'>Only numeric characters</span></td>
    </tr>
</table>

By the looks of the code and the html this only 'notifies' you of invalid input this case non-numeric.

I require something that restricts it - does not let you physically type in any non numerica values.

I have done this in IE using events, but FF im lost.

If I am wrong let me know, any ideas on restricting in FF? Thanks

If u tried my code, it will prevent u to enter any non-numeric characters, i am using both FF and IE6 to test it, and it works, yes u are right it does print out a message telling u that u have to use numeric characters only, but u can take that step out and the function will look like this:

function checkNumeric(entry)
    {
        var numeric = /^[0-9]*$/; 
        entry.value = entry.value.replace(/[^0-9]/g,"");
    }

when u type in there it will remove the characters as u type it in.

what was the html you used?

Thanks Regards, X

It is available higher up on this thread but here it is again:

<table>
<tr>
        <td>Display Limit:</td>
        <td><input type="text" maxlength="5" style="width:95px;" name="displayLimit" onkeyup="checkNumeric(this)" id="displayLimit" value="100" /></td>
    </tr>
</table>

your id/name is just for use in your form dosent suit any purpose for the validation I gather?

Well sounds great thanks for the Info and ill reply soon to let you know if it worked :)

your id/name is just for use in your form dosent suit any purpose for the validation I gather?

I tend to always put the id and name attributes in, whether i use them or not, but for the example it was used in the submission of the form.

I hope it works for u :)

commented: saved me when no one else could! Thankyou so much! +1

Dude your a legend!!!

Thanks so much I gave up on this thought there was no fix :(

Goodwork! Thankyou again!

My pleasure, glad to help. :)

Umm on the topic I was wondering if you have a solution to restrict the input even being entered for both browsers.

Like at the moment u type and any non numeric disappears after being typed (only if it starts as a numeric, sorta works after - eg, 1212a) is it possible to restrict from even entering the text field. Like I press A and nothing happens in the text field?

Just wondering, thanks again.

Can u post your code, coz i tested mine, and it works in IE6 and FF3, doesnt allow any non numeric character input at any point, event after u have inserted some numbers.

u could disable the field, but then if someone makes a mistake with the numbers they cant change the numbers.

Sorry my fault must have been when I was experimenting with keyXXX.

Ive experimented with keyPress, etc keyUp works best.

I was wondering if there was a way just not to let the character appear at all? is that possible or no?

Also I just wanted to confirm a piece of javascript if you dont mind.

var numeric = /^[0-9]*$/

What does that code specifically stand for? (I assume all characters minus 0-9]?

What would it be for just alpha?

What would it be for just special?

What wold it be for just @?

I have found on the internet ascii stuff but hard to find java equivalents for characters.

Thanks, sorry about all the trouble :( X

> var numeric = /^[0-9]*$/ This isn't a valid RegExp since it also validates a blank string. Numeric RegExp validation would go something along the lines of /^[0-9]+$/ or simply /^\d+$/ . [0-9] stands for any numeric character (0-9) where - is the range operator. Regular expressions are different beasts altogether, a single post won't suffice the purpose of explaining it all to you. Read up more on it here.

commented: Very useful post and link. Thankyou! +1

SOS thanks I think I get it know after that simple explaination.

alpha = [A-Z] or [a-z] or [A-z] (or should it be [A-Za-z] not sure?)
numeric = [0-9]
special characters = ???

What is the difference in the above expression:
/^[0-9]*$/ or /^[0-9]+$/ or /^\d+$/

Dont understand them in the fact minus the last one I totally do not understand. The + or * means what in relation to [0-9] appearing?

So alpha I would have done /^[A-Za-z]$/ , that correct?

Also if I use /^[A-Za-z]{2,4}$/ that means an alpha value being 2,4 characters long?

Thanks for the help, Regards X

PS: Is there a reg exp for special characters?

Old Thread - just wondered if anyone has got answers to my previous questions.

Thankyou, Regards X

alpha = [A-Za-z]
numeric = [0-9]
special characters = could be [^A-Za-z0-9], but it depends on your definition of 'special'. usually it's prefered to treat _ ( underscore ) as non-special at minimum, thus: [^A-Za-z0-9_]

when a character group ( enclosed in square brackets ) starts with ^, it means 'not': [^a-z] means "anything but the characters a-z". when ^ is in a character group but not at the start, it means the literal ^ ( caret ) character.. e.g. [^^] means "anything but ^".

when a regex pattern starts with ^; it means "bind to the start of the string*", and it will only be able to match if the regex matches from the very beggining of the string, similarly $ at the end of a regex means it will only be able to match if the regex matches up to the very end of the string, so ^ at the beggining AND $ at the end means only match if the entire regex matches the entire string. ( otherwise, a regex will match if any part of the string matches the rules. )

( * or to any given line, but thats complicated so don't worry about it for now ).


/^[0-9]*$/ means match a complete string that contains 0 or more numbers, this is what * means ( i.e. an empty string, or a string containing only numbers )

/^[0-9]+$/ means match a complete string that contains 1 or more numbers, this is what + means ( i.e. only a string containing numbers, not an empty string )

/^\d+$/ means the same as /^[0-9]+$/ : \d means the same as [0-9]. ( see http://java.sun.com/docs/books/tutorial/essential/regex/pre_char_classes.html )

/^[A-Za-z]{2,4}$/ means 2,3 or 4 alphanumeric characters

See http://www.zytrax.com/tech/web/regex.htm, or any of the other equally informative regex guides out there for more in-depth info.

commented: Great informative reply. Thankyou! +1

Thankyou MattEvans, great informative reply.

so if ^ used in the brackets it means any character and before the brackets(outside) means not?

trying to test with these things atm.

no, ^ as the FIRST character INSIDE the square brackets means that the whole set is negated, i.e. the set of characters is a set to NOT match.. eg:

[^x] means not x

and ^ as a character other than the first character in the square brackets means that a literal ^ is an option:

[x^] means x or ^

if it's outside the brackets, it means something different, and what it means depends on what else is in the regex before that.

Ummm I have been testing it and been using /[^0-9]/ .

The code will only allow me to type in 0-9.

If I remove the ^ it allows me to type anything but 0-9.

Am I doing something wrong?

Sorry, Thanks for the help.

How are you using the result of the regex match? Perhaps you're using the result incorrectly, because that seems backwards.

Check regexes on the tester application here: http://www.zytrax.com/tech/web/regex.htm#experiment... ( in this app, you don't need to include the surrounding slashes "/ /" around the regex )

( EDIT: you should also use ^ and $ around the regex, unless you know exactly what you're doing, e.g. use /^[0-9]$/ and /^[^0-9]$/.. )

Ummm I have been testing it and been using /[^0-9]/ .

The code will only allow me to type in 0-9.

If I remove the ^ it allows me to type anything but 0-9.

Am I doing something wrong?

Sorry, Thanks for the help.

Are you sure you are doing it the right way. In Javacript console of Firebug:

/[^0-9]/.test("a")
true

/[^0-9]/.test("9")
false

What is the environment in which you are testing these regexes?

Hmmm I thought I am doing something wrong the moderators are never wrong :)

I cant find out what im doing wrong though (im guessing the syntax im using them is backwards)...

These are the 3 reg expressions I use:
- /[^0-9]/
- /[^A-Za-z]/
- /[^0-9A-Za-z@-_.]/

Anything wrong with those statements?

As they do exactly what I require.

Thanks for the help, Regards X

Thanx for the codes! it solved mt problem! more power Godbless!

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.