Regex to check if string contains Alphanumeric Characters and Spaces only - javascript - javascript

This is what I have so far:
function checkTitle(){
reg = /^[\w ]+$/;
a = reg.test($("#title").val());
console.log(a);
}
So far in my tests it catches all special characters except _.
How do I catch all special characters including _ in the current function?
I need the string to only have Alphanumeric Characters and Spaces. Appreciate the help cause I am having a hard time understanding regex patterns.
Thanks!

Your problem is that \w matches all alphanumeric values and underscore.
Rather than parsing the entire string, I'd just look for any unwanted characters. For example
var reg = /[^A-Za-z0-9 ]/;
If the result of reg.test is true, then the string fails validation.

Since you are stating you are new to RegExp, I might as well include some tips with the answer. I suggest the following regexp:
/^[a-z\d ]+$/i
Here:
There is no need for the upper case A-Z because of the i flag in the end, which matches in a case-insensitive manner
\d special character represents digits

Related

Regex to match letters and digits in a string optionally having a set of special characters

https://regex101.com/r/RLi59p/1
/(?=.*[0-9])(?=.*[a-z])([a-z0-9]+)[!##$%^&*]*$/i
The regex I made matches following patterns:
Test2123
568test
test2rest
#test2rest
#test2rest#
but it fails to match following patterns
#test2rest#5
#test2rest#test
You need to make little correction in your regex and define all allowed characters together in one like this,
(?=.*[0-9])(?=.*[a-z])[a-z0-9!##$%^&*]*$
In your regex, you have this,
([a-z0-9]+)[!##$%^&*]*
which means in your input string, alphanumeric characters must come first and special characters at last, which is why it was failing.
You're missing a-z from your end of string check, so #test2rest## matches but #test2rest#a does not.
Try:
/(?=.*[0-9])(?=.*[a-z])([a-z0-9]+)[!##$%^&*a-z]*$/i

I'm looking for a regex to detect special characters in javascript so I can show a validation error

I'm looking for a regex to detect special characters in javascript.
current attempt doesnt cater for all special characters:
this.validateSpecialCharacters = (valueA): boolean => {
const regex = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/;
return regex.test(valueA);
};
Does anyone have a better regex to cater for all english keyboard special characters e.g. #!"£$%^&*() etc
Since you are matching every special character, it's better to just tell the regex to not match alphanumeric characters.
const regex = /[^A-Za-z0-9]/
You can add any other cases inside the []. This regex will match anything that is not included inside the [].

Regular expression for validating with specific characters through javascript

I want to validate a string that does not allows the following characters.
<,>,:,","/,\,|,?,*,#
I want to validate this through JavaScript.
I was trying this with the following code.
var reg = /[^a-zA-Z0-9 \-_]+/;
reg.test(filename[0])
But this was unable to validating the symbol #.
Please help.
The problem you have is that you included the hyphen in the middle of the pattern without escaping it. This tells the engine that you are expecting a range--in this case space through underscore. It's easier (in my opinion) to place the hyphen as either the first or last character in the pattern, at which point you don't have to escape it. (It would be the second character if you are using a negated character class.)
e.g.
var reg = /[^a-zA-Z0-9 \-_]+/;
--OR--
var reg = /[^a-zA-Z0-9 _-]+/;
--OR--
var reg = /[^-a-zA-Z0-9 _]+/;
Do you only want to allow English letters a-z (and A-Z), numbers, the space, '_', and '-'? If so, that is different than disallowing the characters you specified since '☃' doesn't have the characters you provided but may not be a valid string in your use case.
In the case you just want the English alphabet, numbers, space, '_', and '-', you can use the following RegExp and conditional:
var reg = /^[a-zA-Z0-9 \-_]+$/;
if (reg.test(filename[0])) {
// String is ok
}
This says everything in the string between beginning (^) and end ($) must be one or more of the allowed characters.
If you want to disallow the characters you provided in your question, you can use:
var reg = /[\<\>\:\,\/\\\|\?\*\#]/;
if (!reg.test(filename[0])) {
// String is ok
}
This says to search for any of the characters you've listed (they are all escaped with a \ before them) and if you find any, the string is invalid. So only if the test fails is the string a valid string - that's why there's a ! before the test.
string sourceString ="something" ;
var outString = sourceString.replace(/[`~!##$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, '');

Regular Expression for alphabets, numbers and symbols

I am looking for a regex for allowing
Alphabets case insensitive [a-zA-Z]
hyphen and underscore [-_]
forward and backward slashes [/\\\\]
numbers [0-9]
Hence
var regex = new RegExp('^[a-zA-Z-_][/\\\\]*$');
regex.test('ABC/90-1_AB');
does not work.
Your current regexp (/^[a-zA-Z-_][/\\\\]*$/) is looking for a string that start with a letter, - or _ who are then followed by 0 or more / or \ that end the string.
Put it inside 1 bracket :
'^[-_/0-9a-zA-Z\\\\]*$'
Try:
var regex = new RegExp('[\w\\/-]','i'); // \w matches alphanumeric characters and underscore
regex.test('ABC/90-1_AB'); // returns true
JSFIDDLE
Since you aren't willing to have complex RegExp why making it difficult, when you can just match your needs with explicitly required symbols

regex that checks that a string is between two lengths and has only letters and numbers

I'm trying to validate a username field in my form via client-side valdiation and I'm having some trouble.
I'm trying to use match them against regexs, which seems to work for my password strength/match. However when I try and change the regular expression to one that is suitable for usernames it doesn't work.
This is the regular expression that works, it checks to see if the length is at least 6 chars long.
var okRegex = new RegExp("(?=.{6,}).*", "g");
This is the other regular expression which does not work:
var okRegex = new RegExp("/^[a-z0-9_-]{3,16}$/");
How do I write a regex that performs username validation? (That it's of a certain length, has only letters and numbers)
You're mixing regex literals with the RegExp constructor. Use one or the other, but not both:
okRegex = new RegExp('^[a-z0-9_-]{3,16}$');
or
okRegex = /^[a-z0-9_-]{3,16}$/;
As #zzzzBow answered you are mixing up two ways of using regular expressions. Choose one or the other. Now, a break down:
^ Matches the beginning of the string (that means that the string must start with whatever follows).
[a-z0-9_-] Matches the charecters a-z, A-Z, digits 0-9 _ (underscore) and - (dash/hyphen).
{3,16} States that there must be 3-16 occurences from the above character class.
$ Matches the end of the string, so the can't be anything after the 16 characters above.
Hope that helps.

Categories

Resources