Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a following requirement to only allow capital letters and , in a javascript form . I am unsure on how to check for special characters and script tags . I have written the following code . I do not want to allow characters such as $,%,& etc .
var upperCase= new RegExp('[A-Z]');
var lowerCase= new RegExp('^[a-z]');
var numbers = new RegExp('^[0-9]');
if($(this).val().match(upperCase) && $(this).val().match(lowerCase) && $(this).val().match(numbers))
{
$("#passwordErrorMsg").html("OK")
}
Based on what you've given us, this may suit the bill. It will determine if any characters are not in character classes a-z, A-Z or 0-9 but note this will also treat é or similar characters as rejected symbols.
So if the value is 'test_' or 'test a' it will fail but it will pass for 'testa'. If you want it to accept spaces change the regex to /[^a-zA-Z0-9 ]/.
if(!/[^a-zA-Z0-9]/.test($(this).val())) {
$("#passwordErrorMsg").html("OK");
}
This may be helpful.
javascript regexp remove all special characters
if the only characters you want are numbers, letters, and ',' then you just need to whitespice all characters that are not those
$(this).val().replace(/[^\w\s\][^,]/gi, '')
This link may be helpful:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
It has a lot of information on JS Regexps.
For a dollar sign ($), the regexp syntax would be: \$. You need to escape the special character so it is read as a literal. The syntax would be the same for the other special characters, I believe.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
How to use regex to replace all the occurrences of space ( . ) '- with underscores.
I used chaining of replaceAll. Is there a better approach?
const str = "Certificate of Naturalization (From N-550 or N-570) or Certificate of U.S Citizenship (From N-560 or N-561)";
console.log(((str).toUpperCase()).replaceAll(' ', '_').replaceAll('\'', '_').replaceAll('(', '').replaceAll(')', '').replaceAll('-', '_').replaceAll('.', '_'));
You can put the characters you want to replace inside [] and you need to escape some special characters with \
there are 12 characters with special meanings: the backslash , the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), the opening square bracket [, and the opening curly brace {, These special characters are often called “metacharacters”. Most of them are errors when used alone.
If you want to use any of these characters as a literal in a regex, you need to escape them with a backslash.
const str = `Certificate of Naturalization (From N-550 or N-570) or Certificate of U.S Citizenship (From N-560 or N-561)`;
const result = str.replace(/[-'\s\(\)\.]/g, '_')
console.log(result)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have textbox and I want get string value. But I want users to not be able to enter the string that has number on first letter. As matter of fact I want to replace number with '' null.
for example
1test =====convert=======> test
you can simply use ^[a-zA-Z]
^ starts with a-z or A-Z
or if you want special character too then use ^\D
^\D : Matches anything other than a decimal digit
Regex Demo
you can use $text.replace(/^[^0-9]+/, '')
/^ beginning of the line
[^0-9]+ match anything other than digits at-least once
thanks # Wiktor and Tushar
here is the solution: You can check on this live regex.
https://regex101.com/r/OJfyv4/1
$re = '/\b[a-z][a-z0-9]*/';
$str = '1test';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
This works your case:
^\d+
https://regex101.com/r/daezA9/1
^ asserts position at start of the string
\d matches a digit (equal to [0-9])
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am going nuts on this for a few days now. I have read and tried many previously answered questions close to this topic, I've fiddled with all kinds of expressions on regexr.com (very awesome page BTW) but I can't for the life of me figure it out.
I want to do the following in JavaScript/Jquery (this has to do with contenteditable="true" elements where I want to restrict user input).
Some elements are for text input without spaces and allowed numbers (abc1_d-ef), some for free text (abc. d2ef, gh6i? j8kl: mno!), some for integers (123), some for decimals (1,2 / 1.2).
BUT I want to always forbid a newline character or tab (\n \r \t \f).
So:
var pattern = new RegExp(.........);
var text = $("#my-id").html();
var test = pattern.test(text);
// test should be true for correct text / integer / decimal
// but should be false as soon as text contains a newline, tab etc.
So basically I'm looking for 4 different expressions:
letters, numbers, underscore, hyphen
letters, numbers, spaces, special characters and such
numbers 0-9
numbers, comma and dot
but none of them with newline etc.
I hope I could make myself clear.
For all your requirements, you can use:
^[-,.\w ]+$
See a demo on regex101.com.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to use regular expression to check whether a password field contains more than two special characters in it.Is it possible to perform this check using regular expression in javascript?If so how?
I think you mean the special characters as _ or any non-word character. The below regex would match the strings which has more than two (atleast three) special characters.
^.*?[\W_].*?[\W_].*[\W_].*$
Example:
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'bar")
false
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'ba:r")
true
> /^.*?[\W_].*?[\W_].*[\W_].*$/.test("foo_'ba:r{}{}[]")
true
If your string matches the regex: /^(?:.*[!*$|#]){3}/ it means that there're 3 or times one of the special characters contained in the character class.
It's up to you to define tthe special characters to include in this character class
x{2,} 2 or more of x
Is probably what you are looking for
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For my website , i need a REGEXP in java script for validation TITLE which can take alphabates, digits and Special char set [, / ( ) & - : . space], but if any user enter only single and double spaces or single or double .. like [..] in title or double digit [1 2] then it's should not allowed, atlest one aplhabate is required. please help
You can use this pattern:
^[-a-z0-9,/()&:. ]*[a-z][-a-z0-9,/()&:. ]*$
This will match any number of your special characters followed by a Latin letter, followed by number of your special characters. It's effectively equivalent to [-a-z0-9,/()&:. ]+ except it requires at least one [a-z] somewhere in the string.
Of course, you need to escape the \ when written as a regex literal in javascript, and you probably want to use the i flag for case-insensitive matching:
var pattern = /^[-a-z0-9,\/()&:. ]*[a-z][-a-z0-9,\/()&:. ]*$/i