Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Allowed characters

1. a-z
2. A-Z
3. -(minus)
4. .(dot) should come in the middle of the string and only once
5. (space)

What I am doing is
^[a-zA-Z0-9'- ]+[\.]?[a-zA-Z0-9'- ]+$

it dosent work. JS error - Invalid range in character set

I even tried escaping the minus still same error

^[a-zA-Z0-9'\- ]+[\.]?[a-zA-Z0-9'\- ]+$

please help
Posted

The error you are getting is because a literal dash in a character set must be first or last. Otherwise it is interpreted as indicating a range:
^[-a-zA-Z0-9' ]+[\.]?[-a-zA-Z0-9' ]+$

I'm assuming you have these more restrictive rules for your filenames for a reason. Other characters are allowed in filenames, and there are standard regexes out there. Even so, I have some comments about your regex in case it's not what you intended:

- You state that there should always be a dot, but your regex doesn't require it. Again, for standard file naming, an extension is not required, so that's OK.

- To match the dot, your [\.]? is overkill. The literal dot needs to be escaped (\.?) or put in a character class ([.]?), but not both.

- Because of the way you've split the character classes with the possible dot in the middle, your regex requires the filename to be at least two characters (at least one matching before the dot, at least one after). If you are trying to have an optional extension, this would be a better representation of the meaning, and allow single-character filenames:
^[-a-zA-Z0-9' ]+(\.[-a-zA-Z0-9' ]+)?$
 
Share this answer
 
v4
Comments
maverick12131 20-Dec-13 2:19am    
its not a mandate to have the dot always.
its just that the dot should be persent only 1 time (zero or one occourance)

also the reg ex you mentioned is not accepting any dots. Not even 1
^[-a-zA-Z0-9' ]+(\.[-a-zA-Z0-9' ]+)?$
Brian A Stephens 20-Dec-13 8:21am    
OK, so my regex is what you need. It allows for the possibility of one dot followed by more letters or numbers. I'm not sure why you're not getting it to work. Can you post your code? If you are using
new RegExp(" ... ")
syntax, be sure you escape the escape character (double backslash) because it's also an escape character within quotes.
maverick12131 21-Dec-13 1:05am    
function Validation() {
$("[id$=txtFileName]").keypress(function(event) {
var regex = new RegExp("^[-a-zA-Z0-9' ]+(\\.[-a-zA-Z0-9' ]+)?$");
if (event.which != 40 && event.which != 41 && event.which != 42 && event.which != 43 && event.which != 44) {
var str = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (regex.test(str))
return true;
}
event.preventDefault();
return false;
})
}

even after escaping the backslash, the reg ex dosen't accept a dot
Brian A Stephens 30-Dec-13 9:40am    
The code tells it all! You are trying to validate each character on its own, but your requirements make this impossible ("dot should come in the middle of the string and only once"). Because of the dot validation, you must consider the entire string, not just one letter. You will need to change this line:
var str = $('#txtFileName').val();

But you still have the problem that keypress happens BEFORE the new text becomes part of the input field value. You need to validate the text that includes the new key. That can only happen in keyup, but that's too late to prevent the key's action.

You need to either validate the entire filename on submit rather than entry, or keep track of a "previous" value for the input so you can revert it back in a keyup handler if the new value doesn't validate.
var filename = 'FileName'; // enter your file name here
alert(filename.match(/^\s*[a-z-._\d,\s]+\s*$/i))
 
Share this answer
 

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