Check whether white spaces exist without using trim - javascript

I have following code that checks whether date is valid. http://jsfiddle.net/uzSU6/36/
If there is blank spaces in date part, month part or year part the date should be considered invalid. For this, currently I am checking the length of string before and after trim operation. It works fine. However is there a better method to check for white spaces? (For example, using === operator)
function isValidDate(s)
{
var bits = s.split('/');
//Javascript month starts at zero
var d = new Date(bits[2], bits[0] - 1, bits[1]);
if ( isNaN( Number(bits[2]) ) )
{
//Year is not valid number
return false;
}
if ( Number(bits[2]) < 1 )
{
//Year should be greater than zero
return false;
}
//If there is unwanted blank space, return false
if ( ( bits[2].length != $.trim(bits[2]).length ) ||
( bits[1].length != $.trim(bits[1]).length ) ||
( bits[0].length != $.trim(bits[0]).length ) )
{
return false;
}
//1. Check whether the year is a Number
//2. Check whether the date parts are eqaul to original date components
//3. Check whether d is valid
return d && ( (d.getMonth() + 1) == bits[0]) && (d.getDate() == Number(bits[1]) );
}

You can use indexOf() function if there is space in the date string.
if(s.indexOf(' ') != -1)
{
//space exists
}

you can test for any whitespace like
if( /\s/g.test(s) )
it will test any whitespace.

You may want to consider using a regexp to test your string's validity:
function isValidDate(s) {
var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
var mdy = s.match(re);
if (!mdy) {
return false; // string syntax invalid;
}
var d = new Date(mdy[3], mdy[1] - 1, mdy[2]);
return (d.getFullYear() == mdy[3]) &&
(d.getMonth() == mdy[1] - 1) &&
(d.getDate() == mdy[2]);
}
The regex does all this in one go:
checks that there are three fields separated by slashes
requires that the fields be numbers
allows day and month to be 1 or 2 digits, but requires 4 for the year
ensures that nothing else in the string is legal
See http://jsfiddle.net/alnitak/pk4wU/

Related

Converting string to number of seconds

I'm trying to create a simple program that converts the numbers of hours provided by the user into seconds, and asks the user to reenter hours ,if he provides a string.It works fine when I enter non-positive number, but doesn't show any message if entered a string value.
Here is the code:
function convertToSeconds () {
var d = prompt("Enter any hour between 0-24","4");
if ( d<0 ){
alert("Please enter a number greater than zero");
convertToSeconds();
}
else if( typeof d == String ) {
/*Problem seems to be here*/
alert(d + " is not a valid number");
convertToSeconds();
}
else {
var seconds = 3600*parseFloat(d);
document.write(seconds);
}
};
convertToSeconds();
The prompt method only returns strings. You need to determine if the string can be converted to a positive integer between 0 and 24, so:
var d = prompt('Enter any hour between 0-24');
if ( /^(1?[0-9]|2[0-4])$/.test(d) ) {
alert('looks good');
} else {
alert('don\'t like that');
}
or you could do something like:
var d = Number(prompt('Enter any hour between 0-24'));
if (d == parseInt(d) && d > -1 && d < 25 {
// ok
}
// not ok
}
Just an issue with your code. typeof "any string" = "string" not String, also JS coerces a number in a string format to a number by default so I would recommend checking for string before number.
function convertToSeconds () {
var d = prompt("Enter any hour between 0-24","4");
if ( d<0 ){
alert("Please enter a number greater than zero");
convertToSeconds();
}
else if( isNaN(d) ) {
/*Problem seems to be here*/
alert(d + "is not a valid number");
convertToSeconds();
}
else {
var seconds = 3600*parseFloat(d);
document.write(seconds);
}
};
convertToSeconds();

Javascript switch

I am working on a zodiac calendar that requires a switch 0-11 for the signs. I have written HTML code that drops down for the month and a text input for the year. The sign should use id 'output' and should also show up in text. I am not sure if I am using my switch correctly, of if my math is causing the problem or why it is not sending to output.
HTML CODE:
<div><label for="sign">Sign</label><input type="text"
name ="sign" id="sign"></div>
Javascript Code
if (year && year.value && (year.length == 4)){
year = parseInt(years.value);
month = parseInt(month.value);
if (month < 2) {
year = (year - 1);
}
year = ((year - 1924) % 12);
} else { // Show Error:
document.getElementById('year').value =
'Please enter valid values.';
}
switch (year){
case 0 :
block code;
break;
etc..
} // End Switch
if (output.textContent != undefined) {
output.textContent = sign;
} else {
output.innerText = sign;
}
return false;
}
Your regular expression could be failing to match your lowercased url. When that happens, the result would be null.
You should be checking the match() result before using it. Something like this:
var matches = url.toLowerCase().match(/https?:\/\/(.+?)[?#\/$]/);
if (!matches || matches.length < 2) {
// Handle error
...
} else {
// Keep going
var domain = matches[1];
...
}
Also, verify that your regular expression is actually doing what you intend.
Because of my javascript code innerText
if (output.textContent != undefined) {
output.textContent = sign;
} else {
output.innerText = sign;
}
I had to delete
<div><label for="sign">Sign</label><input type="text"
name ="sign" id="sign"></div>
and replace it with
<p>Sign: <span id="output"></span></p>
I could have easily changed the javascript code and document.getElementID('output') = sign.value;
The problem should be caused by domain checking instead of calculate function.
Remove domain checking and try again (see if it works).
Errors:
1) if (year && year.value && (year.value.length == 4)){
year = parseInt(year.value);
2) main html didn't declare element "output"

Javascript - Get a date from any kind of string

I would like to know if there is a way (I hope there is one, or I am in trouble :p), to find a date in any kind of string.
Here are some examples to make you understand what I am looking for :
var string1 = 'blabla-test-20140215.dat'
I would need to have access to '20140215'
var string2 = 'blabla_Test.20141212'
I would need to have access to '20141212'
Well, I would like to be able to find a date in a format yyyymmdd in a string, no matter the string.
Thank you if you have any clue, I haven't found anything on internet yet.
EDIT :
There can be other numbers in the string, but always less than 8.
for instance :
var string3 = 'blabla-2526-20141212'
The date I am looking for is always separated from other numbers. I can't have :
var string4 = 'blabla-252620141212'
I just want to find the numbers representing the date (in a format yyyymmdd, for instance in string3 I want 20141212, which refers to : 12/12/2014)
I'm note sure, but if your string contains digits only for date, you can use RexEx ?
may be like this :
var regex = /[0-9]+/;
var string1 ="blabla-test-20140215.dat";
var found = regex.exec(string1);
alert('Found: ' + found);
It is simple enough to extract a sequence of eight numbers from a string with regex, just use something like the following:
var dateString = (/\d{8}/.exec(string) || [])[0];
This will find the first eight-character-long string of numbers in a given string. If no such sequence exists, it will be undefined. You can then use it to create a Date object if necessary.
You should use Regex for this.
re = /(\d{4})(\d{2})(\d{2})/g //create a regex that matches: 4 digits followed by 2 digits followed by 2 digits
///// yyyy mm dd
result = re.exec(string1) //execute the regex
// now, access the different capture groups according to their indexes
console.log(result[1]) // yyyy
console.log(result[2]) // mm
console.log(result[3]) // dd
This solution will check for valid dates (not just any 8 arbitrary numbers):
function hasDate(input) {
var matches = input.match(/(\d{4})(\d{2})(\d{2})/),
indexOf = function (elem, arr) {
var len = arr.length, i;
for (i = 0; i < len; i++) {
if (arr[i] === elem) {
return i;
}
}
return -1;
},
months31 = [1, 3, 5, 7, 8, 10, 12],
idx, isLeapYear,
year, month, day;
if (matches) {
year = parseInt(matches[1]);
month = parseInt(matches[2]);
day = parseInt(matches[3]);
//Check invalid dates from the start
if (month < 1 || month > 12) {
return false;
}
else if (day < 1 || day > 31) {
return false;
}
idx = indexOf(month, months31);
isLeapYear = ((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0);
//Month has 31 days, we are good
if (idx >= 0) {
return true;
}
//Feb - check for leap year
else if (month === 2 && (day <= 28 || (isLeapYear && day <= 29))) {
return true;
}
//All other months
else if (month !== 2 && day <= 30) {
return true;
}
}
//If we got this far, its a bad date
return false;
}
//return true
hasDate('blabla_Test.20141212');
//return false
hasDate('blabla_Test.20140229');
//return true
hasDate('blah_20140228');
//return true
hasDate('blah_20000229');

How do I validate a date in this format (yyyy-mm-dd) using jquery?

I am attempting to validate a date in this format: (yyyy-mm-dd). I found this solution but it is in the wrong format for what I need, as in: (mm/dd/yyyy).
Here is the link to that solution: http://jsfiddle.net/ravi1989/EywSP/848/
My code is below:
function isDate(txtDate)
{
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; //Declare Regex
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray == null)
return false;
//Checks for mm/dd/yyyy format.
dtMonth = dtArray[1];
dtDay= dtArray[3];
dtYear = dtArray[5];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}
What regex pattern can I use for this that will account for invalid dates and leap years?
I expanded just slightly on the isValidDate function Thorbin posted above (using a regex). We use a regex to check the format (to prevent us from getting another format which would be valid for Date). After this loose check we then actually run it through the Date constructor and return true or false if it is valid within this format. If it is not a valid date we will get false from this function.
function isValidDate(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
if(!dateString.match(regEx)) return false; // Invalid format
var d = new Date(dateString);
var dNum = d.getTime();
if(!dNum && dNum !== 0) return false; // NaN value, Invalid date
return d.toISOString().slice(0,10) === dateString;
}
/* Example Uses */
console.log(isValidDate("0000-00-00")); // false
console.log(isValidDate("2015-01-40")); // false
console.log(isValidDate("2016-11-25")); // true
console.log(isValidDate("1970-01-01")); // true = epoch
console.log(isValidDate("2016-02-29")); // true = leap day
console.log(isValidDate("2013-02-29")); // false = not leap day
You could also just use regular expressions to accomplish a slightly simpler job if this is enough for you (e.g. as seen in [1]).
They are build in into javascript so you can use them without any libraries.
function isValidDate(dateString) {
var regEx = /^\d{4}-\d{2}-\d{2}$/;
return dateString.match(regEx) != null;
}
would be a function to check if the given string is four numbers - two numbers - two numbers (almost yyyy-mm-dd). But you can do even more with more complex expressions, e.g. check [2].
isValidDate("23-03-2012") // false
isValidDate("1987-12-24") // true
isValidDate("22-03-1981") // false
isValidDate("0000-00-00") // true
[1]
Javascript - Regex to validate date format
[2] http://www.regular-expressions.info/dates.html
Since jQuery is tagged, here's an easy / user-friendly way to validate a field that must be a date (you will need the jQuery validation plugin):
html
<form id="frm">
<input id="date_creation" name="date_creation" type="text" />
</form>
jQuery
$('#frm').validate({
rules: {
date_creation: {
required: true,
date: true
}
}
});
DEMO + Example
UPDATE: After some digging, I found no evidence of a ready-to-go parameter to set a specific date format.
However, you can plug in the regex of your choice in a custom rule :)
$.validator.addMethod(
"myDateFormat",
function(value, element) {
// yyyy-mm-dd
var re = /^\d{4}-\d{1,2}-\d{1,2}$/;
// valid if optional and empty OR if it passes the regex test
return (this.optional(element) && value=="") || re.test(value);
}
);
$('#frm').validate({
rules: {
date_creation: {
// not optional
required: true,
// valid date
date: true
}
}
});
This new rule would imply an update on your markup:
<input id="date_creation" name="date_creation" type="text" class="myDateFormat" />
Here's the JavaScript rejex for YYYY-MM-DD format
/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))/
try this Here is working Demo:
$(function() {
$('#btnSubmit').bind('click', function(){
var txtVal = $('#txtDate').val();
if(isDate(txtVal))
alert('Valid Date');
else
alert('Invalid Date');
});
function isDate(txtDate)
{
var currVal = txtDate;
if(currVal == '')
return false;
var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Declare Regex
var dtArray = currVal.match(rxDatePattern); // is format OK?
if (dtArray == null)
return false;
//Checks for mm/dd/yyyy format.
dtMonth = dtArray[3];
dtDay= dtArray[5];
dtYear = dtArray[1];
if (dtMonth < 1 || dtMonth > 12)
return false;
else if (dtDay < 1 || dtDay> 31)
return false;
else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
return false;
else if (dtMonth == 2)
{
var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
if (dtDay> 29 || (dtDay ==29 && !isleap))
return false;
}
return true;
}
});
changed regex is:
var rxDatePattern = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/; //Declare Regex
I recommend to use the
Using jquery validation plugin and jquery ui date picker
jQuery.validator.addMethod("customDateValidator", function(value, element) {
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
if (! re.test(value) ) return false
// parseDate throws exception if the value is invalid
try{jQuery.datepicker.parseDate( 'dd-mm-yy', value);return true ;}
catch(e){return false;}
},
"Please enter a valid date format dd-mm-yyyy"
);
this.ui.form.validate({
debug: true,
rules : {
title : { required : true, minlength: 4 },
date : { required: true, customDateValidator: true }
}
}) ;
Using Jquery and date picker just create a function with
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
if (! re.test(value) ) return false
// parseDate throws exception if the value is invalid
try{jQuery.datepicker.parseDate( 'dd-mm-yy', value);return true ;}
catch(e){return false;}
You might use only the regular expression for validation
// dd-mm-yyyy
var re = /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/ ;
return re.test(value)
Of course the date format should be of your region
moment(dateString, 'YYYY-MM-DD', true).isValid() ||
moment(dateString, 'YYYY-M-DD', true).isValid() ||
moment(dateString, 'YYYY-MM-D', true).isValid();
Just use Date constructor to compare with string input:
function isDate(str) {
return 'string' === typeof str && (dt = new Date(str)) && !isNaN(dt) && str === dt.toISOString().substr(0, 10);
}
console.log(isDate("2018-08-09"));
console.log(isDate("2008-23-03"));
console.log(isDate("0000-00-00"));
console.log(isDate("2002-02-29"));
console.log(isDate("2004-02-29"));
Edited: Responding to one of the comments
Hi, it does not work on IE8 do you have a solution for – Mehdi Jalal
function pad(n) {
return (10 > n ? ('0' + n) : (n));
}
function isDate(str) {
if ('string' !== typeof str || !/\d{4}\-\d{2}\-\d{2}/.test(str)) {
return false;
}
var dt = new Date(str.replace(/\-/g, '/'));
return dt && !isNaN(dt) && 0 === str.localeCompare([dt.getFullYear(), pad(1 + dt.getMonth()), pad(dt.getDate())].join('-'));
}
console.log(isDate("2018-08-09"));
console.log(isDate("2008-23-03"));
console.log(isDate("0000-00-00"));
console.log(isDate("2002-02-29"));
console.log(isDate("2004-02-29"));
Rearrange the regex to:
/^(\d{4})([\/-])(\d{1,2})\2(\d{1,2})$/
I have done a little more than just rearrange the terms, I've also made it so that it won't accept "broken" dates like yyyy-mm/dd.
After that, you need to adjust your dtMonth etc. variables like so:
dtYear = dtArray[1];
dtMonth = dtArray[3];
dtDay = dtArray[4];
After that, the code should work just fine.
Working Demo fiddle here Demo
Changed your validation function to this
function isDate(txtDate)
{
return txtDate.match(/^d\d?\/\d\d?\/\d\d\d\d$/);
}
You can use this one it's for YYYY-MM-DD. It checks if it's a valid date and that the value is not NULL. It returns TRUE if everythings check out to be correct or FALSE if anything is invalid. It doesn't get easier then this!
function validateDate(date) {
var matches = /^(\d{4})[-\/](\d{2})[-\/](\d{2})$/.exec(date);
if (matches == null) return false;
var d = matches[3];
var m = matches[2] - 1;
var y = matches[1] ;
var composedDate = new Date(y, m, d);
return composedDate.getDate() == d &&
composedDate.getMonth() == m &&
composedDate.getFullYear() == y;
}
Be aware that months need to be subtracted like this: var m = matches[2] - 1; else the new Date() instance won't be properly made.

Format currency using javascript

a script returns either a number like 0.0580 so in x.xxxx format or a (x) for X units left.
I want to format the number 0.0580 and return 5.8 cent or return x units left.
Any ideas how to do that in javascript? Especially how do I format the x.xxxx?
In case the first x is not 0 I want to return e.g. 1.75$.
MS has written a nice plugin for jquery. it's especially useful if you're localizing. Give it a go:
http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx
I'm not sure if this can be used outside of jquery...
I may be spoiling you here, but whatever. Here's a function that I found somewhere at some point and have been recycling since. I haven't actually bothered to look much into it to figure out what it does exactly, but it has been rather useful:
function FormatMoneyAmount(starting_string, ending_string) {
//check validity of input (true = invalid, false = valid)
var valid_exp = new RegExp ('[^0-9,.$]', 'gi');
input_invalid = (typeof(ending_string) == 'undefined' && valid_exp.test(starting_string));
//check if more than 2 digits follow decimal or no decimal
decimal_invalid = typeof(ending_string) == 'undefined' && (starting_string.indexOf('.') > -1) && ((starting_string.length - starting_string.indexOf('.')) > 3);
if (input_invalid || decimal_invalid) {
ending_string = starting_string;
} else {
//remove commas, dollar signs
var replace_exp = new RegExp ('[,$]', 'gi');
starting_string = starting_string.replace(replace_exp, '');
//remove decimal if ending string not set, save for adding on later
var decimal_substring = '';
if (typeof(ending_string) == 'undefined' && starting_string.indexOf('.') > -1) {
decimal_substring = starting_string.substring(starting_string.indexOf('.'), starting_string.length);
remaining_string = starting_string.substring(0,starting_string.indexOf('.'));
} else {
remaining_string = starting_string;
}
//if string is already 3 characters or less, do nothing
if (remaining_string.length > 3) {
//separate last 3 characters of string from rest of string
var final_three = remaining_string.substring(remaining_string.length - 3, remaining_string.length);
remaining_string = remaining_string.substring(0, remaining_string.length - 3);
//if not first group of 3, add new group before old group with comma, else set to new group
ending_string = (typeof(ending_string) == 'undefined') ? final_three + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring) : final_three + ',' + ending_string;
//call function again if more than 3 digits remaining to process, else add to end string
if (remaining_string.length > 3) {
ending_string = FormatMoneyAmount(remaining_string, ending_string);
} else {
ending_string = remaining_string + ',' + ending_string;
}
} else {
ending_string = (typeof(ending_string) == 'undefined') ? remaining_string : remaining_string + ',' + ending_string + ((typeof(decimal_substring) == 'undefined') ? '' : decimal_substring);
}
}
return ending_string;
}
The first thing to do is check the format of the string, since you will have two code paths depending on the result:
if (typeof num = "string" && num.slice(0,1) == "(" && num.slice(-1) == ")") {
// String is in the format (x), so we just need to return that number
return num.slice(1,-1) + " units left";
}
The next part is to check if the number is less than 1, indicating that it is cents and not whole dollars. If it is less than 1, multiplying it by 100 will give you the number of cents you're after:
if (+num < 1)
// 0.0580 * 100 = 5.8
return (num * 100) + " cents";
else
return +num + "$";

Categories