I am validating website by writing serverside regular expression when i copy the same thing in javascript and try to validate its not happening a part of my code is as follows
"^(https://)+\w+\.+\w" - //serverside validation working fine
document.getElementById('txtWebsite').value.match("^(https://)+\w+\.+\w") == null
how can i implement the same in client side validation
String.match is considering you regex as String and not as a RegExp object.
Try this:
document.getElementById('txtWebsite').value.match(/^(https:\/\/)+\w+\.+\w/)
Related
So I need to make regex validation case insensitive. I know in the Javascript regex engine you can pass in something like this /regex/i to the constructor of RegExp.
The problem is that with ASP RegularExpressionValidator there is nothing to tell it that the regex is case insensitive and the javascript that does the work on the client side does not give any options to pass this flag.
How have you guys gotten around this issue short of creating a regex like [A-Za-z]. As you can imagine this gets very complex and ugly when the regex is complex.
I have a regex created by myself that I am currently running in PHP. Although when I merge it over to JavaScript, it refuses to work. I have also tried it in Python and it works perfectly fine.
Regex:
#[[](.[^]]+)[]][()](\d+)[)]
Testing in PHP, and working
Testing in JavaScript, and not working
JavaScript doesn't automatically escape your ].
This will help you get a visual idea:
PCRE:
JS:
Python:
So to fix this, you need to escape the brackets
#[[](.[^\]]+)[\]][()](\d+)[)]
// ^ ^
The best way to write this regex is to minimize the use of character classes:
#\[(.[^\]]+)\][()](\d+)\)
That's why it's good practice to escape this stuff instead of relying on quirks of the flavor.
I generated these images through regex101.
Problem:-
I am using the following regex to find the special characters in a string
"/[^a-zA-Z0-9\s]/i"
I want to get all the characters that match this pattern and all that is working fine.
The condition is that that I have to use the same expression both in php and javascript.
But the g flag in the above regex is creating problem as preg_match and preg_match_all do not accept this flag and I have to search globally.
Question:-
SO how can I get all the special characters using the same expression both in php and javascript?
You can't use the same regex in both PHP and JavaScript because their regex engines make different assumptions and support different features.
More than just the incompatibility with the g modifier, this regex will fail you if the input contains non-ASCII characters: the input encoding in PHP and JS will be almost certainly different and PHP will not even be Unicode-aware unless you use the u flag (which does not exist in JS because it's Unicode-aware by default).
Just use two different regular expressions.
To match [^a-zA-Z0-9\s] in JavaScript you would have to use:
[\u0000-\u0008\u000F-\u001F\u0022-\u002F\u003B-\u0040\u005C-\u0060\u007C-\u0084\u0087-\u009F\u00A2-\u167F\u1682-\u180D\u1810-\u1FFF\u200C-\u2027\u202B-\u202E\u2031-\u205E\u2061-\u2FFF\u3002-\uFFFF]
Desired Behaviour
I want to be able to validate a single field in a form to check whether it is a valid URL.
By valid URL I mean the following would validate the following formats as well as other TLD's etc:
http://www.somesite.com
http://www.somesite.com/subdir
http://somesite.com
http://somesite.com/subdir
www.somesite.com
www.somesite.com/subdir
The rest of the form validation does not rely on a plugin, so I'd prefer not to use one if possible.
What I've Tried
Standalone Regex
I've tested several of the accepted answers and suggestions regarding validating a URL with jQuery.
For one reason or another they don't seem to work, for example the following which is stated as being the regex from a plugin:
var myVariable = "http://www.google.com/";
if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*#)?((\[(|(v[\da-f]{1,}\.(([a-z]|\d|-|\.|_|~)|[!\$&'\(\)\*\+,;=]|:)+))\])|((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=])*)(:\d*)?)(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*|(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*)?)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*)|((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)){0})(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|\/|\?)*)?$/i.test(myVariable)) {
alert("valid url");
} else {
alert("invalid url");
}
Marks as valid: http://
Marks as invalid: www.site.com
Regex From Plugins
I looked for the regex used in popular plugins such as:
jQuery Validation Plugin - https://github.com/jzaefferer/jquery-validation
where the relevant regex in the source seems to be:
/^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*#)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|\/|\?)*)?$/
To test functionality, I downloaded the master from GitHub and ran demo/index.html and got Firebug console error:
TypeError: $.validator is undefined
I copy and pasted the regex above for use in the first code sample and the following was marked as valid:
http://w.c
and:
h5Validate - https://github.com/dilvie/h5Validate
where the relevant regex in the source is:
/(https?|ftp):\/\/(((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{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$&'\(\)\*\+,;=]|:|#)|\/|\?)*)?/
Using this snippet, again in the context of the first code sample, marked the following as valid:
http://w
Don't Do This or Do It Differently
I've seen posts that state that attempts to validate URL's via regex are futile:
https://stackoverflow.com/a/1305082/1063287
And that it could be done server side (eg with Python):
https://stackoverflow.com/a/161749/1063287
Question
So I'm just wondering, as at the time of posting, if there is a definitive solution for using jQuery to validate a URL without a plugin?
In short: No, because that's not the purpose of the Jquery library (as #Juhana mentioned).
Also, as #Adeneo wrote, clientside validation is for ui purposes only (i.e. telling the user if it's potentially valid or not). Real validation should be serverside.
That said, I recommend using this regex, which is good for ui purposes.
I have this C# regex:
^\s?((?<qty>\d+)\s?/)?\s?[$]?\s?(?<price>\d{0,2}(?:\.\d{1,2})?)\s?$
and use MVC's data validation at the client. JavaScript says this regex is invalid, although C# works perfectly fine. Any idea how to get it to work for both C# and JavaScript, since it doesn't seem possible to provide a separate JavaScript Regex in the Data Validation annotations?
The regex validates a quantity and price. 4/$2.69 for example.
Javascript does not support named reference (?<…>…). You need to use
^\s?((\d+)\s?/)?\s?[$]?\s?(\d{0,2}(?:\.\d{1,2})?)\s?$
and refer qty and price as 1 and 2 instead.
Remove the group names (<qty>).