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

Loại bỏ các ký tự không liên quan Loại bỏ không gian và Dấu gạch ngang. Để loại bỏ bất cứ khoảng trống không mong muốn hoặc dấu gạch ngang từ đầu vào sử dụng, đối tượng của String thay thế phương pháp () có thể được sử dụng để tìm các nhân vật và thay thế bằng gì, như trong ví dụ 17,39 | Form Validation with Regular Expressions 771 EXPLANATION continued 3 4 5 6 7 8 If the user didn t enter anything at all and the field is empty another alert will be sent to the user and false will be returned. The form will not be submitted. If the user entered only alphabetic characters in his or her name true will be returned and the form will be submitted. This is where the HTML form starts. The onSubmit handler will be triggered when the user clicks the Submit button and the okAlpha function will be called passing a reference to the form called alphaTest. The user enters his or her name in the text field called fname. After filling out the form the user will click the Submit button thereby triggering the onSubmit handler on line 6. See Figure . Enter your first name Elhek9 Submit Reset Figure The user has a digit in her name. She can only enter alphabetic characters or she will see the warning. Removing Extraneous Characters Removing Spaces and Dashes. To remove any unwanted spaces or dashes from user input the String object s replace method can be used to find the characters and replace them with nothing as shown in Example . EXAMPLE html head title Removing Spaces and Dashes title head 1 body bgcolor magenta big h2 Removing Spaces and Hyphens h2 script type text javascript var string 444- 33 - 12 34 2 var regex - g 3 var newString regex The original string string Continues From the Library of 772 Chapter 17 Regular Expressions and Pattern Matching EXAMPLE continued br The new string newString br script big body html EXPLANATION 1 The string contains numbers spaces and dashes. 2 The variable called regex is assigned a regular expression which means Search for one or more spaces or dashes globally multiple occurrences within the string . 3 The replaceQ method searches in the string for spaces and dashes and if it finds any replaces them with the empty string .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.