This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how can i validate a url in javascript using regular expression
I want to validate whether user input a valid internet adress like
www.hotmail.com
http://www.abc.com.pk
Here's the js function:
function validateString(regexPattern, testString) {
var regexObj = new RegExp(regexPattern);
return regexObj.test(testString);
}
Finding the regex for URLs is just a matter of typing it in google. Here's a good one I've used before.
Related
This question already has an answer here:
Why this javascript regex doesn't work?
(1 answer)
Closed 2 years ago.
I created function that should validate user input. The value should only be accepted as valid if format is matching this hh:mm:ss.s. Here is the function:
function time_format(time_val) {
let regEx = new RegExp('/^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{2}?$/');
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));
All three values failed the test above. First and second format should pass the regex. The third should fail since the hours are not valid. If anyone knows how this can be fixed please let me know.
Try this…
function time_format(time_val) {
let regEx = /^([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(:|\.)\d{1,2}?$/;
console.log(time_val);
console.log(regEx.test(time_val));
};
console.log(time_format('00:00:00.0'));
console.log(time_format('05:35:23.7'));
console.log(time_format('25:17:07.0'));
This question already has answers here:
Parse XML using JavaScript [duplicate]
(2 answers)
Closed 5 years ago.
I simply need to extract some info between two tags, in this case, <wsse:BinarySecurityToken>
For example:
<wsse:BinarySecurityToken att1="abc" att2="cd3" att3="adfa">This is a text I need!!!===</wsse:BinarySecurityToken>
I tried
match = text.match(/<wsse:BinarySecurityToken[.*]>([^<]*)<\/wsse:BinarySecurityToken>/g)
does't work!
Or is there anything better than regex? I use angularJs 1
You want to do:
match = text.match(/<tag1.*>([^<]*)<\/tag1>/g)
This question already has answers here:
How to check if a string "StartsWith" another string?
(18 answers)
Closed 7 years ago.
I would like to check the url that the user is saving and specially that it needs to be in a special format so i can use it at a later stage. The input field is to save a youtube video link and i would like to make sure that it is an actual youtube link or else present the user with an error stating it is not the proper format.
https://www.youtube.com/watch?v=y4Yp3-llWDs
I need to make sure that the beginning part of the url is proper always starting with this: https://www.youtube.com/watch
What would be the best way to go about this? regex?
var url = 'https://www.YouTube.com/watch'
if (/^https:\/\/www.youtube.com\/watch/i.test(url)) {
console.log('Valid YouTube URL Detected')
} else {
console.error('Invalid YouTube URL Detected')
}
This question already has answers here:
Redirect faster with Greasemonkey (or similar userscript engine)?
(3 answers)
Closed 9 years ago.
i want to redirect from the first link to the other using javascript or jquery in greasemonkey
http://*.tumblr.com/post/*/*
http://$1.tumblr.com/image/$2
Use this regex
/^http:\/\/(.*).twitter.com\/post\/(.*)\/(.*)/g
Like this
var regexp = new RegExp(/^http:\/\/(.*).twitter.com\/post\/(.*)\/(.*)/g);
var results = regexp.exec(window.location.href);
To create your array, then you can
if(results){
window.location="http://"+results[1]+".twitter.com/image/"+results[2]+"/"+results[3];
}
To redirect
Live Version
This question already has answers here:
What is the ultimate postal code and zip regex?
(20 answers)
Closed 10 years ago.
What would be the best way to validate a zip code in jQuery, I'm not talking about if its a 5 character number, I'm talking about a VALID zip code, like it checks with some service to make sure its valid. By a valid zip code I mean a zip code in the USA that belongs to a City and State.
I'm talking about a VALID zip code, like it checks with some service to make sure its valid.
var zipCode = getTheZipCodeFromSomeWhere();
jQuery.getJSON("http://example.com/some-service", { zipCode:zipCode }, function(data) {
updateZipCodeValidity(data.isZipValid);
});
You'll need to define getTheZipCodeFromSomeWhere(), updateZipCodeValidity() and http://example.com/some-service as appropriate to your situation.