ชนิด input: text, password, email, number และ tel
เลือกชนิด input ที่เหมาะสมสำหรับแต่ละช่องข้อมูล
ชนิด input: text, password, email, number และ tel เป็นบทเรียน HTML Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน HTML Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน
องค์ประกอบ input
องค์ประกอบ <input> ใช้สร้างตัวควบคุมฟอร์มได้หลายชนิด แอตทริบิวต์ type จะกำหนดชนิดของตัวควบคุม:
<input type="text"> <!-- single-line text -->
<input type="password"> <!-- masked text -->
<input type="email"> <!-- email address -->
<input type="number"> <!-- numeric -->
<input type="tel"> <!-- phone number -->type=text
ชนิดอินพุตเริ่มต้น ซึ่งเป็นช่องข้อความบรรทัดเดียว:
<label for="username">Username</label>
<input
type="text"
id="username"
name="username"
placeholder="e.g. jane_doe"
maxlength="50"
autocomplete="username"
>type=password
อินพุตรหัสผ่านจะปกปิดอักขระขณะที่ผู้ใช้พิมพ์:
<label for="pwd">Password</label>
<input
type="password"
id="pwd"
name="password"
minlength="8"
autocomplete="current-password"
required
>
<!-- Browser may offer to save/fill the password -->
<!-- autocomplete="new-password" for registration forms -->type=email
อินพุตอีเมลจะตรวจสอบรูปแบบอีเมลบนอุปกรณ์เคลื่อนที่และเดสก์ท็อป:
<label for="email">Email</label>
<input
type="email"
id="email"
name="email"
placeholder="you@example.com"
autocomplete="email"
required
>
<!-- Mobile: shows @ on keyboard -->
<!-- Browser validates basic email format on submit -->type=number
อินพุตตัวเลขยอมรับค่าตัวเลขพร้อมตัวควบคุมสำหรับเพิ่มหรือลดค่า:
<label for="qty">Quantity</label>
<input
type="number"
id="qty"
name="quantity"
min="1"
max="100"
step="1"
value="1"
>
<!-- min, max, step control allowed values -->
<!-- Mobile: shows numeric keypad -->
<!-- Note: only integers with step=1; decimals need step="0.01" -->type=tel
อินพุตโทรศัพท์ยอมรับหมายเลขโทรศัพท์โดยไม่ตรวจสอบรูปแบบ:
<label for="phone">Phone number</label>
<input
type="tel"
id="phone"
name="phone"
placeholder="+1 (555) 555-0100"
pattern="[+][0-9 ()-]{7,20}"
autocomplete="tel"
>
<!-- Mobile: shows phone keypad -->
<!-- No built-in validation — phone formats vary globally -->
<!-- Use pattern attribute for format validation -->แอตทริบิวต์ name สำคัญอย่างยิ่ง
แอตทริบิวต์ name เป็นสิ่งที่เซิร์ฟเวอร์ใช้ระบุแต่ละช่องข้อมูล:
<form action="/search">
<!-- Without name, the field is NOT sent with the form -->
<input type="text" name="q" placeholder="Search">
<button type="submit">Search</button>
</form>
<!-- Submits: /search?q=your+search+term -->
<!-- name="q" → the key in the submitted data -->Placeholder เทียบกับ Label
แอตทริบิวต์ placeholder แสดงข้อความตัวอย่าง แต่ห้ามใช้แทนป้ายกำกับ:
<!-- BAD: placeholder as label -->
<input type="text" placeholder="Enter your name">
<!-- GOOD: label + helpful placeholder -->
<label for="name">Full name</label>
<input type="text" id="name" name="name" placeholder="e.g. Jane Smith">
<!-- Placeholder disappears on typing — screen readers often skip it -->value และ defaultValue
เติมค่าเริ่มต้นให้ช่องอินพุตด้วยแอตทริบิวต์ value:
<input type="text" name="country" value="United States">
<!-- Renders with 'United States' pre-filled -->
<input type="number" name="qty" value="1" min="1" max="10">
<!-- Default quantity of 1 -->disabled และ readonly
มีสองวิธีในการทำให้อินพุตแก้ไขไม่ได้:
<input type="text" value="Fixed value" readonly>
<!-- readonly: user can see and select text, but not edit it -->
<!-- Value IS submitted with the form -->
<input type="text" value="Disabled" disabled>
<!-- disabled: user cannot interact with it at all -->
<!-- Value is NOT submitted with the form -->
<!-- NOT in tab order -->ค่าการเติมข้อมูลอัตโนมัติของอินพุต
ค่าการเติมข้อมูลอัตโนมัติเฉพาะช่วยให้เบราว์เซอร์เติมข้อมูลได้แม่นยำขึ้น:
<input type="text" name="name" autocomplete="name">
<input type="email" name="email" autocomplete="email">
<input type="tel" name="phone" autocomplete="tel">
<input type="text" name="street" autocomplete="address-line1">
<input type="text" name="city" autocomplete="address-level2">
<input type="text" name="zip" autocomplete="postal-code">
<input type="password" autocomplete="new-password">ตรวจสอบความเข้าใจ
ควรใช้อินพุตชนิดใดสำหรับช่องหมายเลขโทรศัพท์
ทบทวน: ชนิดอินพุต
ชนิดอินพุตที่ใช้บ่อย:
type="text"— ข้อความทั่วไป (ค่าเริ่มต้น)type="password"— ข้อความที่ปกปิด ใช้autocomplete="new-password"type="email"— อีเมลพร้อมปุ่ม @ บนอุปกรณ์เคลื่อนที่type="number"— ตัวเลขพร้อม min/max/steptype="tel"— หมายเลขโทรศัพท์พร้อมแป้นกดโทรศัพท์- ใส่
nameและ<label>ที่เชื่อมโยงกันเสมอ
คำถามที่พบบ่อย
บทเรียน “ชนิด input: text, password, email, number และ tel” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ชนิด input: text, password, email, number และ tel” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส HTML Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส HTML Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ชนิด input: text, password, email, number และ tel”
เลือกชนิด input ที่เหมาะสมสำหรับแต่ละช่องข้อมูล คุณปฏิบัติ HTML Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน HTML Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน HTML Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ชนิด input: text, password, email, number และ tel” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน HTML Academy นี้ได้ไหม
ได้ บทเรียน HTML Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- องค์ประกอบ form: action, method และ enctype
- ชนิด input: text, password, email, number และ tel
- องค์ประกอบ label และการเข้าถึง
- องค์ประกอบ button: submit, reset และ button