tailieunhanh - Học JavaScript qua ví dụ part 43

Mẫu đơn giản Xác Nhận Đã bao giờ bạn điền một mẫu đơn để mua một cái gì đó, nhấp nút gửi, chờ đợi, chờ đợi, và sau đó nhận được thông báo lớn màu đỏ nói rằng số thẻ bạn đã nhập là không hợp lệ? Và sau đó sau khi chờ đợi tất cả các điều đó, bạn phải nhập lại toàn bộ hình thức bởi vì tất cả các lĩnh vực đã được thiết lập lại? | Programming Input Devices Controls 401 Simple Form Validation Have you ever filled out a form to buy something clicked the submit button waited waited and then received a big red message saying that the card number you entered was invalid And then after all that waiting you had to retype the entire form because all of the fields were reset By letting JavaScript do some preliminary checking of the form input for obvious mistakes and erroneous information you can save the user a lot of time and aggravation. Then after the preliminary checking is done the form is ready to go off to a server program such as Perl or PHP where it can be further validated and processed. This section will show you a little about validating forms doing preliminary checking to see if a password is the correct length making sure a field isn t empty checking for unwanted characters and more. Chapter 17 Regular Expressions and Pattern Matching shows you how to validate the format for e-mail addresses credit cards zip codes names phone numbers Social Security numbers and the like by using regular expressions a powerful pattern matching tool provided by JavaScript. Checking for Empty Fields. Forms often have mandatory fields that must be filled out before the form can be submitted. The following example checks for empty fields. EXAMPLE html head title An HTML Form and the onSubmit Event Handler title script type text javascript 1 function checkForm yourinfo 2 if null Check for an empty string or null value 3 alert Please type in your name 4 return false else 5 return true script head body b 6 form name info action http cgi-bin bookstuff method post 7 onSubmit return checkForm this big p Continues From the Library of 402 Chapter 11 Working with Forms and Input Devices EXAMPLE Type your name here 8 input type text name namestring size 50 p 9 input type submit value Submit input type reset value Clear

TỪ KHÓA LIÊN QUAN