473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Extracting date, phone, zip and currency after validation

114 100+
After doing some testing with setting a patern and using the ( if (preg_match($pattern, $field) I now have a good idea of editing/validating date, phone, zip and currency test boxes from user input frond on a form.

I now realise that I need to know how to properly extract that data into a variable that I can use in a MySQL table INSERT.

What is the best way when all of these fields are text strings, and can the length of the extraction be controlled. The bigest concern for me is the currency field then followed by the rest.

Thanks in advance for all advice.
Jan 6 '07 #1
6 4983
ronverdonk
4,258 Expert 4TB
When needed, you can store all data in VARCHAR fields. Such as:
  • a telephone no is never > 15 chars, so define that as VARCHAR(15),
  • A ZIP code VARCHAR(10),
  • A currency can be stored as a VARCHAR(15) (all currency symbol, comma's, decimals points included) or as a DECIMAL(15,2) but then you have to strip all comma's and currency symbol before storing it into the db. On the other hand: you can calculate directly with a decimal type field.
  • A DATE defined MySQL field has standard format YYYY-MM-DD. If you want to store your MMDDYY(YY) in a standard type DATE field, you must convert before inserting it. You can also store that MMDDYY(YY) in a VARCHAR(8) type field.
    Same as for currency, when you store in in the standard MySQL DATE format it is easier to use calculations on it.

In general: you can store it all in VARCHAR fields, but when you want to perform arithmetic on it without need to convert first, you should stick to the DATE and DEC formats.

Ronald :cool:
Jan 6 '07 #2
ljungers
114 100+
Thanks Ronald for the very good summary of what and how to store in MySQL. I found what you said very useful.

The phone & zip I will leave as a varchar filed, and if the edit/validation is good then I'll store them.

After giving some thought the currency I wish to store as a DECIMAL(15,2) what is the best way to strip all comma's and currency symbol before I store it.

The date I would like to store it in the MySQL format YYYY-MM-DD. I wish to allow either format MMDDYY(YY) to be entered. What is the cleanest way to convert that format into the MySQL standard date format for storing.

Like you said, going this way allows me to calculate and directly perform arithmetic on it without the need to convert.
Jan 6 '07 #3
ronverdonk
4,258 Expert 4TB
[php]<?php
// currency
$tests = array(" $ 42.52 ",
"$ 4232395",
"4112412",
"239583.52",
"$ 3223.23",
"$123,234.53",
"$123,235",
"$ 123,234,325.23");
$pattern = '/^\s*[$]?\s*((\d+)|(\d{1,3}(\,\d{3})+))(\.\d{2})?\s*$/';
foreach ($tests as $test) {
// US currency
if (preg_match_all($pattern, $test, $m)) {
$curr=str_replace(array(',', ' ', '$'), array('', '', ''), $test );
}
else {
echo "Failed match on $test<br />";
}
}

// MMDDYY : how do you determine the century??
// MMDDYYY to YYYY-MM-DD
$date='10262005';
$dat=sprintf('%4s-%2s-%2s', substr($date,4),substr($date,2),substr($date,0));
echo $dat;
?>[/php]
Ronald :cool:
Jan 6 '07 #4
ljungers
114 100+
Thanks for the code. How would I test for a 2 digit year vers a 4 digit year.
I figure that I'll let the user enter the date in either format MMDDYY or MMDDYYYY
Jan 6 '07 #5
ronverdonk
4,258 Expert 4TB
In that case you have to make an assumption like: any year 40 and higher is 19xx otherwise it is 20xx.
(the following is based on this assumption and on the fact that you already have validated the date):
[php]
<?php
// MMDDYY : convert to MMDDYYY
if (strlen($date) == 6) {
if (substr($date,4,2) < '40')
$cent = '20';
else
$cent = '19';
$date = substr($date,0,4).$cent.substr($date,4,2);
}
// MMDDYYY to YYYY-MM-DD (MySQL DATE() format)
$dat=sprintf('%4s-%2s-%2s', substr($date,4,4),substr($date,2,2),substr($date,0 ,2));
// $dat is the var you insert into your table field with DATE format
?>
[/php]
Ronald :cool:
Jan 6 '07 #6
ljungers
114 100+
Thank you Ronald, and again you come through with a good solution.
Jan 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Master | last post by:
i have a little problem that i can't fix, in the Regional option of the Win2k server control panel i have set the Euro currency and the date format with dd/mm/yyyy but all the peoples that use a...
2
by: javier garcia - CEBAS | last post by:
Hi; Please. Could you tell me the more simple way to extract the 'date' part of a timestamp value. I guess we could extract the year, month, and day subfields and concatenate them. But, isn't...
2
by: Jeff Kiesel | last post by:
Has anyone used three textboxes for phone number input and successfully validated it? One textbox for area code, one for exchange, one for number. (yes, we're only doing US numbers) :o)
1
by: skinnybloke | last post by:
Hi - I have a date field in a Access database in the format YYYY/MM/DD HH:MM I have set up 2 date parameters as below but only want to enter the date and not the time. If I do not enter the time...
10
by: JackM | last post by:
I'm still working on validating the phone numbers that are entered on a form but have come across a problem I don't understand how to fix. I can handle most instances when it's in regular US...
2
by: komaladevi | last post by:
hello all ! Can any one help me in validating US Phone Number , I wrotethe javascript for this to get the phone number as 123-234-1234 but i dont know the reason why i am getting this format only...
2
by: JKChan | last post by:
Hi, I'm an A2 ICT student in second year of college right now and I am working on my database project as my coursework. Its about a dance school with 4 tables Student, Class, Teacher and Enrolment. ...
3
by: ryushinyama | last post by:
I am wanting to remove 1's that Canadian customers put in front of their numbers because when FedEx imports them for shipping they leave the 1 and cut off the last number. Other countries numbers...
2
by: ajaymohank | last post by:
Hello friends............ i am trying to get a validation for email and phone number. in my php project i am doing the validation using java script. but i am not getting validations for email and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.