/*ValidateDate
This functions accepts a date string in the form MM/DD/CCYY and
Checks wether the date is valid. If the passed string is not formated
correctly it will be invalid.
The function always checks for '/' or '-' as a separator character in
addition to the passed separator character.
Parameters:
tcDate - The date string you want to verify
toControl - The control object that is assosiated with the date
tcUserMsg - Massage to display if validation fails. Leave blank if you don't want to notify the user.
tlStayOnF - Stay (don't loose focus) on control if validation fails
tlAllowBlank - Allow blank tcDate passed
tlBlankOnF - Blank control value if validation fails
tcSepChar - Separator Character i.e. '/' or '-' or whatever default is '/'
Return: true or false
Author: Einar Kvandahl
Date: 1/26/2004 */
function ValidateDate (tcDate,toControl,tcUserMsg,tlStayOnF,tlAllowBlank,tlBlankOnF,tcSepChar)
{
focusField=toControl; // Global variable to fix a bug with focus in Firefox when called after an alert.
if (tlAllowBlank && tcDate.length==0)
{
return true
}
if (tcUserMsg == 'default')
{
tcUserMsg = 'Invalid Date. Please enter valid date in the form (MM'+tcSepChar+'DD'+tcSepChar+'YYYY)';
}
// Check if the date is on the form MM/DD/CCYY
firstslash = tcDate.indexOf("/");
secondslash = tcDate.indexOf("/",firstslash+1);
// Check if the date is on the form MM-DD-CCYY
if ( firstslash == -1 && secondslash == -1 )
{
firstslash = tcDate.indexOf("-");
secondslash = tcDate.indexOf("-",firstslash+1);
}
// Check if the date is on the form MM'tcSepChar'DD'tcSepChar'CCYY
if ( firstslash == -1 && secondslash == -1 )
{
firstslash = tcDate.indexOf(tcSepChar);
secondslash = tcDate.indexOf(tcSepChar,firstslash+1);
}
if ( firstslash == -1 || secondslash == -1 )
{
if (!(toControl==undefined || toControl=='') && tlBlankOnF){toControl.value = ""}
if (!(tcUserMsg==undefined || tcUserMsg=='')){alert(tcUserMsg)}
if (!(toControl==undefined || toControl=='') && tlStayOnF){setTimeout("focusField.focus();",1);}
return false
}
else
{
// separate month, day and year from the string
cMonth = tcDate.substring(0,firstslash);
cDay = tcDate.substring(firstslash+1,secondslash);
cYear = tcDate.substring(secondslash+1,tcDate.length);
// add 0 before if only one digit for the month is given
if (cMonth.length == 1){cMonth = "0"+cMonth};
// add 0 before if only one digit for the day is given
if (cDay.length == 1){cDay = "0"+cDay};
// if only two digits for the year is given assume that it is 2000 something
if (cYear.length == 2){cYear = "20"+cYear};
// Warning! the getMonth() function starts with 0 as Jan
var test = new Date(cYear,cMonth-1,cDay);
if ( (test.getFullYear() == cYear) &&
(cMonth == test.getMonth()+1) &&
(cDay == test.getDate()) )
{
if (!(toControl==undefined || toControl==''))
{
toControl.value = cMonth+tcSepChar+cDay+tcSepChar+cYear;
return true
}
}
else
{
if (!(toControl==undefined || toControl=='') && tlBlankOnF){toControl.value = ""}
if (!(tcUserMsg==undefined || tcUserMsg=='')){alert(tcUserMsg)}
if (!(toControl==undefined || toControl=='') && tlStayOnF){setTimeout("focusField.focus();",1);}
return false
}
}
}
I need to create something similar (unless built-in function exists) that would validate an input to be positive integer number. Any ideas?
Thanks a lot in advance.
Beware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
function CheckPositive()
{
if(parseInt(document.getElementById('TEXTBOX').value) > 0)
alert('Number is positive');
else
alert('Number is not positive');
I see, the second function removes all entries except for positive digits from the user input. I'm going to try ParseInt, right now writing the adaptation on this using as much of original ValidateDate function as possible.
Thank you both, give me some time to try it out.
Beware of bugs in the above code; I have only proved it correct, not tried it. (Donald Knuth)
Naom
All-Star
36004 Points
7901 Posts
Validate input to be only positive integer number - is there a simple script (build in JavaScript...
Apr 30, 2009 05:31 PM|LINK
Hi everybody,
I have the following JavaScript
/*ValidateDate
This functions accepts a date string in the form MM/DD/CCYY and
Checks wether the date is valid. If the passed string is not formated
correctly it will be invalid.
The function always checks for '/' or '-' as a separator character in
addition to the passed separator character.
Parameters:
tcDate - The date string you want to verify
toControl - The control object that is assosiated with the date
tcUserMsg - Massage to display if validation fails. Leave blank if you don't want to notify the user.
tlStayOnF - Stay (don't loose focus) on control if validation fails
tlAllowBlank - Allow blank tcDate passed
tlBlankOnF - Blank control value if validation fails
tcSepChar - Separator Character i.e. '/' or '-' or whatever default is '/'
Return: true or false
Author: Einar Kvandahl
Date: 1/26/2004 */
function ValidateDate (tcDate,toControl,tcUserMsg,tlStayOnF,tlAllowBlank,tlBlankOnF,tcSepChar)
{
focusField=toControl; // Global variable to fix a bug with focus in Firefox when called after an alert.
if (tlAllowBlank && tcDate.length==0)
{
return true
}
if (tcSepChar==undefined || tcSepChar=='' || tcSepChar.length > 1)
{
tcSepChar = '/';
}
if (tcUserMsg == 'default')
{
tcUserMsg = 'Invalid Date. Please enter valid date in the form (MM'+tcSepChar+'DD'+tcSepChar+'YYYY)';
}
// Check if the date is on the form MM/DD/CCYY
firstslash = tcDate.indexOf("/");
secondslash = tcDate.indexOf("/",firstslash+1);
// Check if the date is on the form MM-DD-CCYY
if ( firstslash == -1 && secondslash == -1 )
{
firstslash = tcDate.indexOf("-");
secondslash = tcDate.indexOf("-",firstslash+1);
}
// Check if the date is on the form MM'tcSepChar'DD'tcSepChar'CCYY
if ( firstslash == -1 && secondslash == -1 )
{
firstslash = tcDate.indexOf(tcSepChar);
secondslash = tcDate.indexOf(tcSepChar,firstslash+1);
}
if ( firstslash == -1 || secondslash == -1 )
{
if (!(toControl==undefined || toControl=='') && tlBlankOnF){toControl.value = ""}
if (!(tcUserMsg==undefined || tcUserMsg=='')){alert(tcUserMsg)}
if (!(toControl==undefined || toControl=='') && tlStayOnF){setTimeout("focusField.focus();",1);}
return false
}
else
{
// separate month, day and year from the string
cMonth = tcDate.substring(0,firstslash);
cDay = tcDate.substring(firstslash+1,secondslash);
cYear = tcDate.substring(secondslash+1,tcDate.length);
// add 0 before if only one digit for the month is given
if (cMonth.length == 1){cMonth = "0"+cMonth};
// add 0 before if only one digit for the day is given
if (cDay.length == 1){cDay = "0"+cDay};
// if only two digits for the year is given assume that it is 2000 something
if (cYear.length == 2){cYear = "20"+cYear};
// Warning! the getMonth() function starts with 0 as Jan
var test = new Date(cYear,cMonth-1,cDay);
if ( (test.getFullYear() == cYear) &&
(cMonth == test.getMonth()+1) &&
(cDay == test.getDate()) )
{
if (!(toControl==undefined || toControl==''))
{
toControl.value = cMonth+tcSepChar+cDay+tcSepChar+cYear;
return true
}
}
else
{
if (!(toControl==undefined || toControl=='') && tlBlankOnF){toControl.value = ""}
if (!(tcUserMsg==undefined || tcUserMsg=='')){alert(tcUserMsg)}
if (!(toControl==undefined || toControl=='') && tlStayOnF){setTimeout("focusField.focus();",1);}
return false
}
}
}
I need to create something similar (unless built-in function exists) that would validate an input to be positive integer number. Any ideas?
Thanks a lot in advance.
(Donald Knuth)
Visit my blog
Microsoft Community Contributor 2011-12
om_prakashp
Contributor
2584 Points
378 Posts
Re: Validate input to be only positive integer number - is there a simple script (build in JavaSc...
Apr 30, 2009 05:39 PM|LINK
Javascript to check for Positive numbers:
function CheckPositive()
{
if(parseInt(document.getElementById('TEXTBOX').value) > 0)
alert('Number is positive');
else
alert('Number is not positive');
}
budugu
All-Star
41122 Points
6021 Posts
Re: Validate input to be only positive integer number - is there a simple script (build in JavaSc...
Apr 30, 2009 05:54 PM|LINK
Here is one of doing with regex..
<script type = "text/javascript" language = "javascript"> function CheckTextBox(i) { if(i.value.length>0) { i.value = i.value.replace(/[^\d]+/g, ''); } } </Script>"Don't be afraid to be wrong; otherwise you'll never be right."
Naom
All-Star
36004 Points
7901 Posts
Re: Validate input to be only positive integer number - is there a simple script (build in JavaSc...
Apr 30, 2009 05:59 PM|LINK
I see, the second function removes all entries except for positive digits from the user input. I'm going to try ParseInt, right now writing the adaptation on this using as much of original ValidateDate function as possible.
Thank you both, give me some time to try it out.
(Donald Knuth)
Visit my blog
Microsoft Community Contributor 2011-12
Naom
All-Star
36004 Points
7901 Posts
Re: Validate input to be only positive integer number - is there a simple script (build in JavaSc...
Apr 30, 2009 07:11 PM|LINK
Thanks a lot, parseInt did the trick.
(Donald Knuth)
Visit my blog
Microsoft Community Contributor 2011-12