 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 10th, 2004, 04:18 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
regexp problem
i have a textbox that will let user to enter the product's price but i have to check if user enter two decimal meaning
12.30, 9.55, 1478.90 is true
12.3, 9.568, 1478.9807 is false
function currency(textbox)
{
if ( /[^0-9]/.test(textbox.value) )
{
alert("Please enter string");
textbox.select();
textbox.focus();
}
}
How can i change the function above to check if the user enter the right price?
|
|

March 10th, 2004, 06:34 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Try this which forces user to enter at least one digit before the decimal point, plus exactly 2 digits after it:
Code:
if ( !/^\d+\.\d{2}$/.test(textbox.value) )
{
alert("Please enter currency");
textbox.select();
textbox.focus();
}
|
|

March 10th, 2004, 08:51 AM
|
|
Authorized User
|
|
Join Date: Oct 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks! It works. But could you help me figure out the code below. I found it somewhere in the web to validate date but it didn't work.
i entered 01/01/2004, 01-01-2004, 01.01.2004 but the alert box keeps appearing.
<html>
<head>
<title>Testing</title>
<script>
function validateUSDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
valid dates with 2 digit month, 2 digit day,
4 digit year. Date separator can be ., -, or /.
Uses combination of regular expressions and
string parsing to validate date.
Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy
PARAMETERS:
strValue - String to be tested for validity
RETURNS:
True if valid, otherwise false.
REMARKS:
Avoids some of the limitations of the Date.parse()
method such as the date separator character.
*************************************************/
var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
//check to see if in correct format
if(!objRegExp.test(strValue))
{
alert("wrong date!");
return false; //doesn't match pattern, bad date
}
else{
var strSeparator = strValue.substring(2,3) //find date separator
var arrayDate = strValue.split(strSeparator); //split date into month, day, year
//create a lookup for months not equal to Feb.
var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
var intDay = parseInt(arrayDate[1]);
//check if month value and day value agree
if(arrayLookup[arrayDate[0]] != null) {
if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
return true; //found in lookup table, good date
}
//check for February
var intYear = parseInt(arrayDate[2]);
var intMonth = parseInt(arrayDate[0]);
if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
return true; //Feb. had valid number of days
}
return false; //any other values, bad date
}
</script>
</head>
<body>
<form name="myform" >
DAte<input id="textbox" type="text" name="datedata" onBlur="validateUSDate(this);"><br><br>
<input type="button" value="Submit" > <br><br>
</form>
</body>
</html>
|
|

March 10th, 2004, 09:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Seems to be an error here var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2} \1\d{4}$/.
Try
Code:
/^\d{1,2}(-|\/|\.)\d{1,2}(-|\/|\.)\d{4}$/
. Not a great expression but then dates are difficult.
--
Joe
|
|

March 10th, 2004, 11:10 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Personally I don't use regexp for date validation, I use something like this instead:
Code:
function CheckValidDate(sValueToTest)
{
var cYEAR = 2, cMONTH = 1, cDAY = 0; // ### change these for other date formats
var bOK = true;
// attempt to convert to a date (assume dd/mm/yyyy format)
var dtValueToTest;
var aDate;
try
{
// ### change this for other date formats ###
aDate = sValueToTest.split("\/");
dtValueToTest = new Date(aDate[cYEAR], aDate[cMONTH] - 1, aDate[cDAY]);
if ( isNaN(dtValueToTest) )
{
bOK = false;
}
}
catch (e)
{
bOK = false;
}
// if the date conversion succeeded, check its the actual date that was input
// (e.g. it will accept 31/2/2003 but will change it to 3/3/2003)
if (bOK)
{
// is it the same year?
if (aDate[cYEAR] != dtValueToTest.getFullYear())
bOK = false;
// is it the same month?
if (aDate[cMONTH] != 1 + dtValueToTest.getMonth())
bOK = false;
// is it the same day?
if (aDate[cDAY] != dtValueToTest.getDate())
bOK = false;
}
return (bOK);
}
Note that this function validates UK/Euro date formats (dd/mm/yyyy), I've indicated where you need to change it for other formats...
hth
Phil
|
|

March 11th, 2004, 04:38 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
At the end of the day you're better off using a date control and forcing sensible choices that way. They're are plenty of nice JavaScript ones available.
--
Joe
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| RegExp Problem |
Abbas |
Classic ASP Professional |
0 |
August 2nd, 2006 10:48 AM |
| RegExp |
crmpicco |
Javascript How-To |
0 |
July 7th, 2005 05:59 AM |
| RegExp |
crmpicco |
Classic ASP Basics |
0 |
June 6th, 2005 05:54 AM |
| RegExp |
Nitin_sharma |
Javascript |
2 |
November 30th, 2004 07:21 AM |
| regexp problem |
godhsf80 |
PHP How-To |
2 |
March 10th, 2004 04:54 AM |
|
 |