Regular expression for alphanumeric in Angularjs - javascript

I want a regex for alphanumeric characters in angularJS, I've tried some regex like "(\d[a-z])" but they allow me to enter only number as well as only alphabets. But I want a regex which won't allow me to enter them.
Example:
121232, abchfe, abd()*, 42232^5$ are some example of invalid input.
12fUgdf, dGgs1s23 are some valid inputs.

This one requires atleast one of each (a-z, A-Z and 0-9):
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]+)$

You can try this one. this expression satisfied at least one number and one character and no other special characters
^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$
in angular can test like:
$scope.str = '12fUgdf';
var pattern = new RegExp('^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$');
$scope.testResult = pattern.test($scope.str);
PLUNKER DEMO

If you wanted to return a replaced result, then this would work:
var a = 'Test123*** TEST';
var b = a.replace(/[^a-z0-9]/gi,'');
console.log(b);
This would return:
Test123TEST
OR
/^([a-zA-Z0-9 _-]+)$/
the above regex allows spaces in side a string and restrict special characters.It Only allows a-z, A-Z, 0-9, Space, Underscore and dash.

try this one : ^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]+$
dGgs1s23-valid
12fUgdf-valid,
121232-invalid,
abchfe-in valid,
abd()*- invalid,
42232^5$- invalid

Related

Regular expression prevent non English letters from email

In our project, we use this regular expression to validate emails:
"^([\w-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,7}|[0-9]{1,3})(\]?)$"
But it allows non English characters.
For example:
"مستخدم#mail.com"
"userمحمد#mail.com"
"userName#خادم.com"
are valid emails.
How to add another rule to this expression to limit inputs to English letters only?
You can omit the alternation | in your pattern, and there is an optional closing bracket \]? which I think you don't need in an email address.
This part in the regex with Javascript and C# [\w-\.] does not seem to be a valid range in a character.
Instead of using \w you can use [A-Za-z0-9] to match ASCII chars and digits 0-9 in C#.
If you don't want to match consecutive dots or hyphens, you can use a pattern like this and then extend it if you have more characters that you want to allow:
^[A-Za-z0-9]+(?:[.-][A-Za-z0-9]+)*#[A-Za-z0-9]+(?:[.-][A-Za-z0-9]+)*\.[a-z]{2,}$
Regex demo
Note that this only validates an email address of this format.
Can do like this
string[] StrInputNumber = { "pradeep1234#yahoo.in", "مستخدم#mail.com'", "userمحمد#mail.com", "userName#خادم.com" };
Regex ASCIILettersOnly = new Regex(#"^[\P{L}A-Za-z]*$");
foreach (String item in StrInputNumber) {
if (ASCIILettersOnly.IsMatch(item)) {
Console.WriteLine(item + " ==> valid");
}
else {
Console.WriteLine(item + " ==>not valid");
}
}
Output
for some basic explanation about regex Click Here
You can use this website to test your regular expression
If you don't need to keep your current expression you can use this one instead:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$.
I tested it with your examples and it works as you want.

Regex issues with word-replacing filter in angular

I am using the filter below to convert the string to camel case. In addition to this I do not want a string with alphanumeric characters to be converted to camelcase.
For example:
If the input is "HELLO I AM INDIA1237"
The output has to be "Hello I Am INDIA1237"
my filter is as below:
angular.module('app')
.filter('titleCase', function() {
return function(input) {
input = input || '';
input = input.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); //convert to camelcase
return input.replace(/[A-Za-z0-9 ]/, function(txt){return txt.toUpperCase();}); //retain alphanumeric string in uppercase
};
});
the second condition does not seem to be working. Can anyone please help.
Your regular expressions are not correct.
\w matches alphanumeric characters, so your first regex should not use that rule. You probably want: /[A-Za-z]+/
Your second regex has two things wrong. Firstly, it only matches one character. Secondly it it will always match the same things the first one does.
You want a regex that only matches words that have a digit. So you need something like: /[A-Za-z]+[0-9][A-Za-z]*/. This will match one or more letters, followed by at least one digit, followed by zero or more letters or digits. If these words always end only in digits, then you could simplify it to /[A-Za-z]+[0-9]+/.

include a special character (!,#,#,&) not include other special characters

i want to test the password field and will update the html for result.
One of them is:
include a special character (!,#,#,&) not include other special characters
i have test first condition like this
reg= new RegExp('(?=.*[!##&])');
var regexmatch=reg.test(password);
can anyone tell me how to test this condition in one regex
From what I understand, you mean this:
/^[a-z\d!##&]+$/i
This only allows letters, numbers and the 4 symbols.
(?=.*[!##&])(?!.*[^!##&])
This should do it for you.The negative lookahead will not allow other special characters.
reg= new RegExp('(?=.*[!##&])(?!.*[^!##$])');
var regexmatch=reg.test(password);
If alphanumerics is allowed
^(?=.*[!##&])(?!.*[^!##&a-zA-Z0-9\n])[a-zA-Z0-9!##&]+$
Try this.See demo.It will allow only alphanumerics and !##&.
https://regex101.com/r/eS7gD7/32

javascript regex for special characters

I'm trying to create a validation for a password field which allows only the a-zA-Z0-9 characters and .!##$%^&*()_+-=
I can't seem to get the hang of it.
What's the difference when using regex = /a-zA-Z0-9/g and regex = /[a-zA-Z0-9]/ and which chars from .!##$%^&*()_+-= are needed to be escaped?
What I've tried up to now is:
var regex = /a-zA-Z0-9!##\$%\^\&*\)\(+=._-/g
but with no success
var regex = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]+$/g
Should work
Also may want to have a minimum length i.e. 6 characters
var regex = /^[a-zA-Z0-9!##\$%\^\&*\)\(+=._-]{6,}$/g
a sleaker way to match special chars:
/\W|_/g
\W Matches any character that is not a word character (alphanumeric & underscore).
Underscore is considered a special character so
add boolean to either match a special character or _
What's the difference?
/[a-zA-Z0-9]/ is a character class which matches one character that is inside the class. It consists of three ranges.
/a-zA-Z0-9/ does mean the literal sequence of those 9 characters.
Which chars from .!##$%^&*()_+-= are needed to be escaped?
Inside a character class, only the minus (if not at the end) and the circumflex (if at the beginning). Outside of a charclass, .$^*+() have a special meaning and need to be escaped to match literally.
allows only the a-zA-Z0-9 characters and .!##$%^&*()_+-=
Put them in a character class then, let them repeat and require to match the whole string with them by anchors:
var regex = /^[a-zA-Z0-9!##$%\^&*)(+=._-]*$/
You can be specific by testing for not valid characters. This will return true for anything not alphanumeric and space:
var specials = /[^A-Za-z 0-9]/g;
return specials.test(input.val());
Complete set of special characters:
/[\!\#\#\$\%\^\&\*\)\(\+\=\.\<\>\{\}\[\]\:\;\'\"\|\~\`\_\-]/g
To answer your question:
var regular_expression = /^[A-Za-z0-9\!\#\#\$\%\^\&\*\)\(+\=\._-]+$/g
How about this:-
var regularExpression = /^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{6,}$/;
It will allow a minimum of 6 characters including numbers, alphabets, and special characters
There are some issue with above written Regex.
This works perfectly.
^[a-zA-Z\d\-_.,\s]+$
Only allowed special characters are included here and can be extended after comma.
// Regex for special symbols
var regex_symbols= /[-!$%^&*()_+|~=`{}\[\]:\/;<>?,.##]/;
This regex works well for me to validate password:
/[ !"#$%&'()*+,-./:;<=>?#[\\\]^_`{|}~]/
This list of special characters (including white space and punctuation) was taken from here: https://www.owasp.org/index.php/Password_special_characters. It was changed a bit, cause backslash ('\') and closing bracket (']') had to be escaped for proper work of the regex. That's why two additional backslash characters were added.
Regex for minimum 8 char, one alpha, one numeric and one special char:
/^(?=.*[A-Za-z])(?=.*\d)(?=.*[!##$%^&*])[A-Za-z\d!##$%^&*]{8,}$/
this is the actual regex only match:
/[-!$%^&*()_+|~=`{}[:;<>?,.##\]]/g
You can use this to find and replace any special characters like in Worpress's slug
const regex = /[`~!##$%^&*()-_+{}[\]\\|,.//?;':"]/g
let slug = label.replace(regex, '')
function nameInput(limitField)
{
//LimitFile here is a text input and this function is passed to the text
onInput
var inputString = limitField.value;
// here we capture all illegal chars by adding a ^ inside the class,
// And overwrite them with "".
var newStr = inputString.replace(/[^a-zA-Z-\-\']/g, "");
limitField.value = newStr;
}
This function only allows alphabets, both lower case and upper case and - and ' characters. May help you build yours.
This works for me in React Native:
[~_!##$%^&*()\\[\\],.?":;{}|<>=+()-\\s\\/`\'\]
Here's my reference for the list of special characters:
https://owasp.org/www-community/password-special-characters
If we need to allow only number and symbols (- and .) then we can use the following pattern
const filterParams = {
allowedCharPattern: '\\d\\-\\.', // declaring regex pattern
numberParser: text => {
return text == null ? null : parseFloat(text)
}
}

Regular expression for not allowing spaces in the input field

I have a username field in my form. I want to not allow spaces anywhere in the string. I have used this regex:
var regexp = /^\S/;
This works for me if there are spaces between the characters. That is if username is ABC DEF. It doesn't work if a space is in the beginning, e.g. <space><space>ABC. What should the regex be?
While you have specified the start anchor and the first letter, you have not done anything for the rest of the string. You seem to want repetition of that character class until the end of the string:
var regexp = /^\S*$/; // a string consisting only of non-whitespaces
Use + plus sign (Match one or more of the previous items),
var regexp = /^\S+$/
If you're using some plugin which takes string and use construct Regex to create Regex Object i:e new RegExp()
Than Below string will work
'^\\S*$'
It's same regex #Bergi mentioned just the string version for new RegExp constructor
This will help to find the spaces in the beginning, middle and ending:
var regexp = /\s/g
This one will only match the input field or string if there are no spaces. If there are any spaces, it will not match at all.
/^([A-z0-9!##$%^&*().,<>{}[\]<>?_=+\-|;:\'\"\/])*[^\s]\1*$/
Matches from the beginning of the line to the end. Accepts alphanumeric characters, numbers, and most special characters.
If you want just alphanumeric characters then change what is in the [] like so:
/^([A-z])*[^\s]\1*$/

Categories