I need validation for Full name text box which should avoid numeric and special characters, but allow space between words. See my JavaScript code here:
function jsPreventNumeric(obj) {
obj.value = obj.value.replace(/[^a-z]/gmi, '').replace(/\s+/g, '');
}
It's working fine except it's not allowing space between words. I don't have much knowledge to edit that regex. Can anyone help me to adjust this regex to allow space also?
\s is the escape sequence for whitespace. So your second replace removes all whitepspaces explicitly. /[^a-z]/ means all characters that are not characters from a-z. Add \s so it only removes characters that are not a-z or whitespace
function jsPreventNumeric(obj){
obj.value.replace(/[^a-z\s]/gmi,"");
}
I dont see the picture. Try this: ^[\\p{L} .'-]+$
Problem solved with:
obj.value = obj.value.replace(/[^a-z\s]/gmi, "");
Related
I've a textbox in an ASP.NET application, for which I need to use a regular expression to validate the user input string. Requirements for regex are -
It should allow only one space between words. That is, total number of spaces between words or characters should only be one.
It should ignore leading and trailing spaces.
Matches:
Test
Test abc
Non Matches:
Test abc def
Test abc --> I wanted to include multiple spaces between the 2 words. However the editor ignores these extra spaces while posting a question.
Assuming there must be either one or two 'words' (i.e. sequences of non-space characters)
"\s*\S+(\s\S+)?\s*"
Change \S to [A-Za-z] if you want to allow only letters.
Pretty straightforward:
/^ *(\w+ ?)+ *$/
Fiddle: http://refiddle.com/gls
Maybe this one will do?
\s*\S+?\s?\S*\s*
Edit: Its a server-encoded regex, meaning that you might need to remove one of those escaping slashes.
How about:
^\s*(\w+\s)*\w+\s*$
Hi guys I'm trying to check if user input string contains a space. I'm using http://regexr.com/ to check if my regular expression is correct. FYI new to regex. Seems to be correct.
But it doesn't work, the value still gets returned even if there is a space. is there something wrong with my if statement or am I missing how regex works.
var regex = /([ ])\w+/g;
if (nameInput.match(regex)||realmInput.match(regex)) {
alert('spaces not allowed');
} else {
//do something else
}
Thanks in Advance
This regex /([ ])\w+/g will match any string which contain a space followed by any number of "word characters". This won't catch, for example, a space at the end of the string, not followed by anything.
Try using /\s+/g instead. It will match any occurrence of at least one space (including tabs).
Update:
If you wish to match only a single space this will do the trick: / /g. There's no real need for the brackets and parenthesis, and since one space is enough even the g flag is kind of obsolete, it could have simply been / /.
Your current regex doesn't match 'abc '(a word with space character at the end) . If you want to make sure, you can trim you input before check :).
You can check here https://regex101.com/
The right regex for matching only white space is
/([ ])/g
I'm making a dictionary application and need an regexp that check if the users input is only letters and spaces eventually. This is probably the most easiest regexp but i can figure it out. So far i have
/^[\w\D]$/
which is not working :/
sorry guys, forgot to mention that will need to exclude all spec characters also.
You seem to want this one :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/
It should accept only characters (including "not English" characters like the ones you have in Spanish and Cyrillic) as well as spaces, but exclude digits.
Example :
/^[\u00C0-\u1FFF\u2C00-\uD7FFa-zA-Z\s]+$/.test("переполнения стека")
returns true
Your regular expression matches exactly one such character.
You can add the + modifier to match one or more characters.
To match a string consisting only of letters and whitespace characters, you can use:
/^[a-zA-Z\s]+$/
var validate = /^[##&%][a-zA-Z0-9]{4}$/;
I need to allow the spaces as well in this Regex.
It's exactly as easy as one might think: add a space to the regex where you want to match a space. Spaces work just like any other character in regex.
If you want to match any whitespace character (including tabs, etc.) you can use \s to match any whitespace character.
You need to do this:
var validate = /^[##&%][a-zA-Z0-9 ]{4}$/;
Note, just add a space within the brackets.
if you want just regular space to be allowed then use a normal space in your brackets like this
/^[##&%][a-zA-Z0-9 ]{4}$/
else if you wish to allow all white spaces such as tab or new line use \s:
/^[##&%][a-zA-Z0-9\s]{4}$/
I am using below code snippet to validate my input string with: only capital letters, numbers and two special characters (those are & and Ñ) & without any space between.
var validpattern = new RegExp('[^A-Z0-9\d&Ñ]');
if (enteredID.match(validpattern))
isvalidChars = true;
else
isvalidChars = false;
Test 1: "XAXX0101%&&$#" should fail i.e isvalidChars = false; (as it contains invalid characters like %$#.
Test 2: "XAXX0101&Ñ3Ñ&" should pass.
Test 3: "XA 87B" should fail as it contains space in between
The above code is not working, Can any one help me rectifying the above regex.
This is happening because you have a negation(^) inside the character class.
What you want is: ^[A-Z0-9&Ñ]+$ or ^[A-Z\d&Ñ]+$
Changes made:
[0-9] is same as \d. So use
either of them, not both, although it's not incorrect to use both, it's redundant.
Added start anchor (^) and end
anchor($) to match the entire
string not part of it.
Added a quantifier +, as the
character class matches a single
character.
^[A-Z\d&Ñ]+$
0-9 not required.
if you want valid patterns, then you should remove the ^ in the character range.
[A-Z0-9\d&Ñ]
Using jquery we could achieve the same in one line:
$('#txtId').alphanumeric({ allow: " &Ñ" });
Using regex (as pointed by codaddict) we can achieve the same by
var validpattern = new RegExp('^[A-Z0-9&Ñ]+$');
Thanks everyone for the precious response added.