tailieunhanh - JavaScript Bible 5th Edition 2004 phần 9

Những yếu tố này chia sẻ nhiều tài sản và phương pháp với các yếu tố bảng ở chỗ chúng đều chứa hàng. Lợi ích của việc xác định phân đoạn bảng là rõ ràng nếu bạn sử dụng quy tắc bảng (xem tài sản trước đó trong chương này) | BC224 Part VI Bonus Chapters Trying to write JavaScript that accommodates all of the world s date and time formats for validation is an enormous if not wasteful challenge. It s one thing to validate that a text box contains data in the form xx xx xxxx but there are also valid value concerns that can get very messy on an international basis. For example while North America typically uses the mm dd yyyy format a large portion of the rest of the world uses dd mm yyyy with different delimiter characters as well . Therefore how should your validation routine treat the entry 20 03 2002 Is it incorrect because there are not 20 months in a year or is it correct as March 20th To query a user for this kind of information I suggest you divide the components into individually validated fields separate text objects for hours and minutes or make select element entries whose individual values can be assembled at submit time into a hidden date field for processing by the database that needs the date information. Alternately you can let your server CGI handle the conversion. Despite my encouragement to divide and conquer date entries there may be situations in which you feel it s safe to provide a single text box for date entry perhaps for a form that is used on a corporate intranet strictly by users in one country . You see some more sophisticated code later in this chapter but a quick-and-dirty solution runs along these lines 1. Use the entered data for example in mm dd yyyy format as a value passed to the new Date constructor function. 2. From the newly created date object extract each of the three components month day and year into separate numeric values with the help of parseInt . 3. Compare each of the extracted values against the corresponding date month and year values returned by the date object s getDate getMonth and getFullYear methods adjusting for zero-based values of getMonth . 4. If all three pairs of values match the entry is apparently valid. Listing 43-8a puts .

TỪ KHÓA LIÊN QUAN