jQuery - Regex to prevent any special characters - javascript

I'm using jQuery's validation plugin to validate a form.
I have a field which should only contain letters or numbers. I have tried the following regex but it's still allowing characters such as ' ; :
[a-zA-Z 0-9]$
What am I doing wrong?
Thanks in advance

Try this:
^[a-zA-Z 0-9]*$
If this is a required field, then change asterisk to plus:-
^[a-zA-Z 0-9]+$

Try this expression::
^[a-zA-Z 0-9]+$

Related

Incorrect Regex Expression

I found this site:
https://mathiasbynens.be/demo/url-regex
and wanted to use for my url validation the regex from the #diegoperini, because according to the table provided on the top of the site, it is the best regex.
When I try to use it, I get a range value error.
P.S. I am using the following Regex expression:
_^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$_iuS
and the following online validator:
http://regexr.com/
It does show the error place in the regex, but I don't know how to manage it. I tried to swap the both ranges, but it doesn't do the trick.
I would appreciate some help.
P. P. S.
I use the regex in the AngularJS directive to validate url input.
Buried within your character classes, you have this range:
\x{00a1}-\x{ffff}
But it should be:
\u00a1-\uffff
Your expression \x{00a1}-\x{ffff} is not the correct syntax for a hex encoding or a character and as-is means any of "x{}0a1f" plus the range "}-x", but "x" is less than "}" so an error is raised to that effect.
This should work
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

validation in javascript for escape sequence

I have textBox which accept alphabeti want to validate the textBox contain proper Drive Path or not using javascript
ex:Suppose Textbox contain 'D:\' then it's valid or else it's invalid...I need to check textbox contain ':\' or not after alphabet
plz help me
try to use javascript method "replace" http://www.w3schools.com/jsref/jsref_replace.asp to remove unwanted contents.
You can use following regex
/^(\\(\\[^\s\\]+)+|([A-Za-z]:(\\)?|[A-z]:(\\[^\s\\]+)+))(\\)?$/

javascript textbox not to allow special characters

I have a problem in validating Textbox in which I have to enter time.
in that only the symbol
":" is allowed,
text also should not allow.
how to do this..
and
I am following the pattern /^\d{1,2}:\d{2}$/
if anyone help me would be appreciated.
thanx in advance.
try and use following regular expression for validating HH:MM time format
^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$
try below pattern:
/^\d{2,}:\d{2}:\d{2}$/

How to make sure white space after 3 characters using regular expressions

Actually, I want to validate the Canadian postal code form field using jQuery validation.
so i add the below method to validate Canadian postal code
//Canada zipcode validation methode
jQuery.validator.addMethod("canadazipRegex", function(value, element) {
return this.optional(element) || /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/i.test(value);
}, "<br>You must enter your postal code in this format: A#A #A#");
using this regular Expression
^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[
]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$
the field should allow only this format
A#A #A#
like :A1A 1A1
Not like:A1A1A1
So,How can i make sure white space after 3 characters.
I am not good with regular Expressions. if you give me good tutorial links also appreciated
Thanks in advance !
Don't make the space optional :
^[ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z]\s[0-9][ABCEGHJ-NPRSTV-Z][0-9]$
Also there are no needs to use quantifier {1}
\x20 matches will spaces. Try using that

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:
^.{0,1000}$
It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?
The problem is that . doesn't match the newline character. I suppose you could use something like this:
^[.\r\n]{0,1000}$
It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?
Obligatory jwz quote:
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.
What you wish is this:
/^[\s\S]{0,1000}$/
The reason is that . won't match newlines.
A better way however is to not use regular expressions and just use <text area element>.value.length
If you just want to verify the length of the input wouldn't it be easier to just verify the length of the string?
if (input.length > 1000)
// fail validation

Categories