Skip to main content

Form Valiidation using Html 5 and Css3

Freelance IT Trainer, Bandung, jakarta, Bali, yogya

Tutorial

In HTML5 forms got a major upgrade with the addition of some simple, yet flexible validation attributes. To support these added attributes CSS3 also added several new pseudo selectors styling controls based on their validation state.

Adding validation



To illustrate the new attributes and some of the new input types, we’ll be building up a simple sign up form. Every sign up form is essentially the same, you fill in your details and click submit. How many times has the form been reset on you after you’ve missed a required value or not provided a strong enough password?

initial

To help prevent this, the first attribute we’ll be using is the required attribute. Adding this to a input box will prevent the form from submitting until a value has been provided.

<form name="signup-form"> <label for="firstname">Firstname:</label> <input id="firstname" name="firstname" type="text" required /> <label for="surname">Surname:</label> <input id="surname" name="surname" type="text" required /> <label for="email">Email:</label> <input id="email" name="email" type="text" required /> <label for="website">Website:</label> <input id="website" name="website" type="text" /> <label for="password">Password:</label> <input id="password" name="password" type="password" /> <input type="submit" value="Signup!" /> </form> 


The next attribute we’ll be adding is the autofocus attribute. This attribute automatically assigns focus to the form on page load. This is a simple enhancement, but one that makes life so much easier.

<input id="firstname" name="firstname" type="text" required autofocus /> 


Next up is the placeholder attribute. This places a sample value or hint in the input box. Depending on the browser, clicking or typing in the input will automatically hide it, but it will re-appear if the input is emptied again

<input id="email" name="email" type="text" placeholder="jonny" required /> 


HTML5 also introduced a few new input types, the first one we’ll be using is the email type. This performs basic validation on the value to ensure it meets standard email format rules.

<input id="email" name="email" type="email" placeholder="jonny" required /> 


Another input type that was added was the url type, as with the email type it makes sure that a valid url including http:// or https:// strings are included.

<input id="website" name="website" type="url" placeholder="http://schnittger.me" /> 


The final attribute we’ll be looking at is the pattern attribute. The pattern attribute allows you to specify a regular expression that will be used to validate the input value. This gives you the option of doing some pretty powerful client-side validation. In this example, we’re going to make sure the password field has at least 1 lowercase, 1 uppercase value and is at least 6 characters long.

<input id="password" name="password" type="password" pattern="^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z]).*$" placeholder="******" required /> 


One attribute that has been tweaked in Chrome and Opera is the title attribute. In Chrome/Opera the title attribute will be appended to the validation error message. This means you can slightly customize the message for your users.

The final form looks like this

<form name="signup-form"> <label for="firstname">Firstname:</label> <input id="firstname" name="firstname" type="text" title="Please enter your firstname" placeholder="Jonny" autofocus required /> <label for="surname">Surname:</label> <input id="surname" name="surname" type="text" title="Please enter your surname" placeholder="Schnittger" required /> <label for="email">Email:</label> <input id="email" name="email" type="email" title="Please enter your email address" placeholder="jonny" required /> <label for="website">Website:</label> <input id="website" name="website" type="url" title="Please enter the url to your website (optional)" placeholder="http://schnittger.me" /> <label for="password">Password:</label> <input id="password" name="password" type="password" title="Please enter a password, it must contain at least 1 lowercase and 1 uppercase character and be at least 6 characters in length" pattern="^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z]).*$" placeholder="******" required /> <input type="submit" value="Signup!" /> </form> 


invalid

Customizing validation styles



CSS3 introduced several new pseudo selectors to help style forms based on their validation state. We’ll be looking at the following four


  • :invalid

  • :valid

  • :required

  • :optional



Using these selectors you can provide clear, visual guides to what exactly is incorrectly filled out in a form. Here I am styling invalid inputs with a red background and valid ones with a green background. I’m also providing a more complex selector to display and asterisk image in required inputs while maintaining the colored background. Finally I’m styling optional fields in a blue.

input:not([type=submit]):invalid { background-color: #ffdddd; } input:not([type=submit]):valid { background-color: #ddffdd; } input:not([type=submit]):invalid:required { background: #ffdddd url('http://developerdrive.developerdrive.netdna-cdn.com/wp-content/uploads/2013/08/asterisk1.png') no-repeat right top; } input:not([type=submit]):valid:required { background: #ddffdd url('http://developerdrive.developerdrive.netdna-cdn.com/wp-content/uploads/2013/08/asterisk1.png') no-repeat right top; } input:not([type=submit]):optional { background-color: #add1ef; } 


final

freelance IT Trainer , Hery - 081223344506

Comments

Popular posts from this blog

Sewa Ruang Meeting Daerah Citarum Bandung

Sewa ruang meeting kantor, training di Bandung Hubungi 0816-4038-78. Lokasi Ruang Meeting berada di Jl.Pelajar Pejuang 45 No.23B 1. Paket per Jam      - Biaya : Rp.50.000 / org / jam     - Fasilitas : Snack & Drink 2. Paket Half-Day     - Waktu : 09.00 s/d 13.00 Wib     - Biaya : Rp.120.000 / org      - Fasilitas : Makan siang 1x, Snack & Drink 3. Paket Full Day     - Waktu : 09.00 s/d 16.00 Wib     - Biaya : Rp.170.000 / org      - Fasilitas : Makan siang 1x, Coffe break 2x, Snack & Drink 4. Lain-lain     - Sewa Proyektor : Rp.150.000/day     - Extra time : Rp.100.000/Jam Syarat dan Ketentuan : 1. Min. 3org dan Max. 7org 2. Pembayaran H-3 sebelum tgl pelaksanaan 3. Melebihi jam ketentuan dikenakan biaya tambahan Sewa Ruang Meeting Daerah Citarum Bandung Hubungi 0816-4038-78 Ruang meeting murah mulai 50.000 di Buah Batu, Pasteur, Sukajadi, Lengkong, Antapani, Uj...

reaksi Blesteran Bule Di Pijitin Tukang Urut. - Youtube | www.pijat.xyz

Reaksi Blesteran Bule Di Pijitin Tukang Urut. - Youtube Yang Ditemukan Pada Tautan www.pijat.xyz Adalah Update Tukang Urut Terdekat Dari Lokasi Saya Terbaru Pada Hari Ini 11 Jun 2020 Yang Terkait Dengan Pencarian Tukang Urut Terdekat Dari Lokasi Saya Mengenail Detail reaksi Blesteran Bule Di Pijitin Tukang Urut. - Youtube Dan Beberapa Ulasan Lain Dari tukang Urut Terdekat Dari Lokasi Saya Sudah Menjadi Trending Populer Di Internet, Lalu Apakah Arti Atau Yang Dimaksud Dengan Reaksi Blesteran Bule Di Pijitin Tukang Urut. - Youtube Seperti Yang Ditemukan Pada Website Www.pijat.xyz Dan Dimanakah Lokasi Informasi Terkait Tukang Urut Terdekat Dari Lokasi Saya ? Apakah Headline Utama Dari Tukang Urut Terdekat Dari Lokasi Saya , Trending Topik Saat Ini Atau Info Reaksi Blesteran Bule Di Pijitin Tukang Urut. - Youtube Adalah Informasi Tukang Urut Terdekat Dari Lokasi Saya Plus Plus Di Kota Anda, Silahkan Akses Detailnya Dengan Download Gratis Terkait Tukang Urut Terdekat Dari Loka...

Lowongan Trainer SEO & Internet Marketing (Freelance Bandung), Fee Rp 600 rb/ Hari

SISINDOTEK sebuah perusahaan yang bergerak di bidang IT Training & Solution Provider di Bandung membutuhkan Tenaga trainer untuk subject : Trainer SEO & Internet Marketing Bandung - Jawa Barat Requirements: D3/ S1 Jurusan Informatika (Terbuka untuk mahasiswa yang mahir SEO) Berpengalaman mengajar minimal 1 tahun atau menguasai SEO dengan baik Berdomisili di Bandung dan sekitarnya Menguasai Teknik dan metode SEO dengan baik Bisa menunjukkan prestasi atas SKILL SEO yang dimiliki (Top Rangking Google atas Blog atau Web yang pernah ditangani SEO nya) Memiliki nilai tambah jika pernah memenangkan Kontes SEO Kompensasi : Kompensasi Rp 450.000 - 700.000 / Hari (Tergantung skill, pengalaman dan sertifikasi) * Waktu mengajar : 09.00 - 17.00 diselingi 2x coffee break dan 1 x lunch selama 3 hari berturut turut. CV lengkap di kirim ke : hrd sebelum 20 Juni 2012, hanya yang memenuhi persyaratan yang akan dipanggil untuk mengikuti sesi Interview dan Test mengajar