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.
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:
How do I reference a JavaScript object property with a hyphen in it?
(11 answers)
Closed 2 years ago.
One of the JSON field in my JSON file has the field "AlphaWorkStatusChangeInfo-Comment" and I'm trying to add value to that field. But some how the type/JS script is not accepting the "-" in the field.
let data = JSON.parse(JSON.stringify(Agri.data));
data.AlphaWorkStatusChangeInfo = ["other"];
data.AlphaWorkStatusChangeInfo-Comment = "Had to quit the job";
data.autoSave = true;
What are my options to include/Add the value in the field "AlphaWorkStatusChangeInfo-Comment".
Any help is much appreciated.
The hyphen is not valid unless you encase it as a string element like so:
data["AlphaWorkStatusChangeInfo-Comment"] = "Had to quit the job";
See here for more discussion on this topic: Which characters are valid/invalid in a JSON key name?
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:
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 7 years ago.
<script language="Javascript">
function monthlyPayment (form) {
var down = form.dPayment.value;
var trade = form.tradeIn.value;
var totalDown = down + trade;
alert ("Total down is " + totalDown);
}
</script>
This is a beginner question as I'm new to Javascript... but I am just trying to make a mock up of a feature I'm trying to implement on my website.
I have a form with two user definable variables for 'Down Payment' and 'Trade-In'. Everything in the code works, except for when it "add's" the numbers (such as $100 + $200), it doesn't output $300, but instead $100200. When I change the sign to multiplication it outputs a correct value.
What am I missing? Is there some .sum or .math code I need to implement? Or is my entire script screwed?
Thank you all for your time and help.
This is a very common mistake people new with javascript make.
the + sign is used to concatenate in javascript, which explains your result is 100200 when you try. What you give him is string, so he just concatenates the two.
use the Number() function to make sure their types become "Number" so your addition will work correctly.
var totalDown = Number(down) + Number(trade);
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.