المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : شرح طريقة استخدام الjquery فى عمليات التحقق للحقول والازرار والاختيارات form validation



Chakira
11-01-2013, بتوقيت غرينيتش 03:20 PM
http://www.dzbatna.com/images/icons/iconrote.gif ط´ط±ط* ط§ط³طھط®ط¯ط§ظ… ط§ظ„jquery ظپظ‰ ط¹ظ…ظ„ظٹط§طھ ط§ظ„طھط*ظ‚ظ‚ ظ„ظ„ط*ظ‚ظˆظ„ ظˆط§ظ„ط§ط²ط±ط§ط± ظˆط§ظ„ط§ط®طھظٹط§ط±ط§طھ form validation (http://www.dzbatna.com/t1413223/)



بسم الله الرحمن الرحيم

هذا شرح طريقة تفصيلى لكيفية استخدام الjquery فى عمليات التحقق للفورمز او اختيار مربع او تفعيل زر او تعطيله,

والشرح طريقة بأذن الله حاولت ان اجعله سلس حتى يتم استيعابه بشكل جيد بأذن الله >>>عامل فيها استاذ http://www.dzbatna.com/images/smilies/shiny01.gif


اولا: ما المقصود بvalidation؟

كثيرا ما تشاهد عندما تقوم بتسجيل عضوية او ملا اى فراغات, يطلب منك حد ادنى للحروف لليوزر
وايضا تطابق الباسورد باللى قبله, هل فكرت كيف يتم فعل ذلك؟ اكيد بمكتبة jquery اللذيذهhttp://www.dzbatna.com/images/smilies/icon30.gif

طيب , مثلا عندنا فورم تسجيل عضوية جديدة register.php ونريد دمج jquery واومره فيه حتى نستغل
الvalidation بدل ما نستخدم اوامر الphp المٌطولة............

اولا: انظر لهذه الصورة اللى سويتها وانا شارح فيها فكرة دمج الjuqery على اى صفحة تستخدمها...




http://i48.tinypic.com/s1u52h.jpg


تمام , صورة ولا احلى..........http://www.dzbatna.com/images/smilies/icon30.gif


بعد ما عرفت الفكرة , دعنا نبدأ نسوى مثال عملى:

اولا: راح نستخدم ملفين js
1-ملف المكتبة jquery.js
2-ملف الvalidation وهو jquery.validate.js

نستدعيهم فى اول الصفحة:

رمز Code:
<script src=&quot;lib/jquery.js&quot; type=&quot;text/javascript&quot;></script> <script src=&quot;jquery.validate.js&quot; type=&quot;text/javascript&quot;></script>

معاهم ملف css طبعا

رمز Code:
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;css/screen.css&quot; />

الحين راح ندمج اوامر الvalidation بين وسم

رمز Code:
<script type=&quot;text/javascript&quot;> اوامر التحقق</script>

راجع الصورة ثم انظر للكود code وراح تجد الفكرة بسيطة:

رمز Code:
$.validator.setDefaults({ submitHandler: function() { alert(&quot;submitted!&quot;); } }); $().ready(function() { // validate the comment form when it is submitted $(&quot;#commentForm&quot;).validate(); // validate signup form on keyup and submit $(&quot;#signupForm&quot;).validate({ rules: { firstname: &quot;required&quot;, lastname: &quot;required&quot;, username: { required: true, minlength: 2 }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: &quot;#password&quot; }, email: { required: true, email: true }, topic: { required: &quot;#newsletter:checked&quot;, minlength: 2 }, agree: &quot;required&quot; }, messages: { firstname: &quot;من فضلك اكتب اسمك&quot;, lastname: &quot;من فضلت اكتب لقبك&quot;, username: { required: &quot;من فضلك اكتب اليوزر&quot;, minlength: &quot;Your username must consist of at least 2 characters&quot; }, password: { required: &quot;من فضلك اكتب الباسورد&quot;, minlength: &quot;Your password must be at least 5 characters long&quot; }, confirm_password: { required: &quot;من فضلك اعد كتابة الباسورد&quot;, minlength: &quot;Your password must be at least 5 characters long&quot;, equalTo: &quot;اعد كتابة الباسورد مثل السابق&quot; }, email: &quot;من فضلك اضف ايميل صالح&quot;, agree: &quot;من فضلك اختار موافقتك لشروطنا&quot; } }); // propose username by combining first- and lastname $(&quot;#username&quot;).focus(function() { var firstname = $(&quot;#firstname&quot;).val(); var lastname = $(&quot;#lastname&quot;).val(); if(firstname && lastname && !this.value) { this.value = firstname + &quot;.&quot; + lastname; } }); //code to hide topic selection, disable for demo var newsletter = $(&quot;#newsletter&quot;); // newsletter topics are optional, hide at first var inital = newsletter.is(&quot;:checked&quot;); var topics = $(&quot;#newsletter_topics&quot;)[inital ? &quot;removeClass&quot; : &quot;addClass&quot;](&quot;gray&quot;); var topicInputs = topics.find(&quot;input&quot;).attr(&quot;disabled&quot;, !inital); // show when newsletter is checked newsletter.click(function() { topics[this.checked ? &quot;removeClass&quot; : &quot;addClass&quot;](&quot;gray&quot;); topicInputs.attr(&quot;disabled&quot;, !this.checked); }); }); </script>

ويتبقى اوامر الcss ثم الفورم الخاص بنا

وهذا هو كود code صفحتنا ككل:

رمز Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;> <html> <head> <meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=windows-1256&quot;> <meta name=&quot;author&quot; content=&quot;&quot;> <link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;screen&quot; href=&quot;css/screen.css&quot; /> <script src=&quot;lib/jquery.js&quot; type=&quot;text/javascript&quot;></script> <script src=&quot;jquery.validate.js&quot; type=&quot;text/javascript&quot;></script> <script type=&quot;text/javascript&quot;> $.validator.setDefaults({ submitHandler: function() { alert(&quot;submitted!&quot;); } }); $().ready(function() { // validate the comment form when it is submitted $(&quot;#commentForm&quot;).validate(); // validate signup form on keyup and submit $(&quot;#signupForm&quot;).validate({ rules: { firstname: &quot;required&quot;, lastname: &quot;required&quot;, username: { required: true, minlength: 2 }, password: { required: true, minlength: 5 }, confirm_password: { required: true, minlength: 5, equalTo: &quot;#password&quot; }, email: { required: true, email: true }, topic: { required: &quot;#newsletter:checked&quot;, minlength: 2 }, agree: &quot;required&quot; }, messages: { firstname: &quot;من فضلك اكتب اسمك&quot;, lastname: &quot;من فضلت اكتب لقبك&quot;, username: { required: &quot;من فضلك اكتب اليوزر&quot;, minlength: &quot;Your username must consist of at least 2 characters&quot; }, password: { required: &quot;من فضلك اكتب الباسورد&quot;, minlength: &quot;Your password must be at least 5 characters long&quot; }, confirm_password: { required: &quot;من فضلك اعد كتابة الباسورد&quot;, minlength: &quot;Your password must be at least 5 characters long&quot;, equalTo: &quot;اعد كتابة الباسورد مثل السابق&quot; }, email: &quot;من فضلك اضف ايميل صالح&quot;, agree: &quot;من فضلك اختار موافقتك لشروطنا&quot; } }); // propose username by combining first- and lastname $(&quot;#username&quot;).focus(function() { var firstname = $(&quot;#firstname&quot;).val(); var lastname = $(&quot;#lastname&quot;).val(); if(firstname && lastname && !this.value) { this.value = firstname + &quot;.&quot; + lastname; } }); //code to hide topic selection, disable for demo var newsletter = $(&quot;#newsletter&quot;); // newsletter topics are optional, hide at first var inital = newsletter.is(&quot;:checked&quot;); var topics = $(&quot;#newsletter_topics&quot;)[inital ? &quot;removeClass&quot; : &quot;addClass&quot;](&quot;gray&quot;); var topicInputs = topics.find(&quot;input&quot;).attr(&quot;disabled&quot;, !inital); // show when newsletter is checked newsletter.click(function() { topics[this.checked ? &quot;removeClass&quot; : &quot;addClass&quot;](&quot;gray&quot;); topicInputs.attr(&quot;disabled&quot;, !this.checked); }); }); </script> <style type=&quot;text/css&quot;> #commentForm { width: 500px; } #commentForm label { width: 250px; } #commentForm label.error, #commentForm input.submit { margin-left: 253px; } #signupForm { width: 670px; } #signupForm label.error { margin-left: 10px; width: auto; display: inline; } #newsletter_topics label.error { display: none; margin-left: 103px; } </style> <title>مثال للتحقق من ملأ الفراغات وعدد الاحرف وتفعيل الازرار</title> </head> <body> <form dir=&quot;rtl&quot; class=&quot;cmxform&quot; id=&quot;signupForm&quot; method=&quot;get&quot; action=&quot;&quot;> <fieldset> <legend>تسحيل عضوية جديدة</legend> <p> <label for=&quot;firstname&quot;>الاسم</label> <input id=&quot;firstname&quot; name=&quot;firstname&quot; /> </p> <p> <label for=&quot;lastname&quot;>اللقب</label> <input id=&quot;lastname&quot; name=&quot;lastname&quot; /> </p> <p> <label for=&quot;username&quot;>اليوزر</label> <input id=&quot;username&quot; name=&quot;username&quot; /> </p> <p> <label for=&quot;password&quot;>الباسورد</label> <input id=&quot;password&quot; name=&quot;password&quot; type=&quot;password&quot; /> </p> <p> <label for=&quot;confirm_password&quot;>أعد كتابة الباسورد</label> <input id=&quot;confirm_password&quot; name=&quot;confirm_password&quot; type=&quot;password&quot; /> </p> <p> <label for=&quot;email&quot;>الايميل</label> <input id=&quot;email&quot; name=&quot;email&quot; /> </p> <p> <label for=&quot;agree&quot;>لقد قبلت شروط الانضمام</label> <input type=&quot;checkbox&quot; class=&quot;checkbox&quot; id=&quot;agree&quot; name=&quot;agree&quot; /> </p> <p> <label for=&quot;newsletter&quot;>ارغب باستلام جديد المقالات على بريدى</label> <input type=&quot;checkbox&quot; class=&quot;checkbox&quot; id=&quot;newsletter&quot; name=&quot;newsletter&quot; /> </p> <fieldset id=&quot;newsletter_topics&quot;> <legend>اختار نوعية المقالات التى تريدها الوصول على بريدك(اختار 2على الاقل)</legend> <label for=&quot;topic_marketflash&quot;> <input type=&quot;checkbox&quot; id=&quot;topic_marketflash&quot; value=&quot;marketflash&quot; name=&quot;topic&quot; /> اسلاميات </label> <label for=&quot;topic_fuzz&quot;> <input type=&quot;checkbox&quot; id=&quot;topic_fuzz&quot; value=&quot;fuzz&quot; name=&quot;topic&quot; /> برامــج </label> <label for=&quot;topic_digester&quot;> <input type=&quot;checkbox&quot; id=&quot;topic_digester&quot; value=&quot;digester&quot; name=&quot;topic&quot; /> اخبار الطب </label> <label for=&quot;topic&quot; class=&quot;error&quot;>Please select at least two topics you'd like to receive.</label> </fieldset> <p> <input class=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Submit&quot;/> </p> </fieldset> </form> <h1 id=&quot;banner&quot;><a href=&quot;http://vistacompany.org/&quot;>vista-design</a> vistacompany</h1> </body> </html>


وفى النهاية هذه صورة للمجلدات :

http://i45.tinypic.com/2e3255f.png



مثال عملى :


اضغــــــط هـــــــــنا DEMO (http://vistacompany.org/vistavalid/)


الملفات ملحقه بالموضوع


أتمنى يكون الدرس مفصل مفهوم

تحياتى vista-design






التعديل الأخير كان بواسطة vista-design; 15 - 12 - 2014 الساعة 02:04

ألعاب الأندرويد مجانا و حصريا (http://www.apotox.info/forum)


الملفات المرفقة
http://www.traidnt.net/vb/images/attach/zip.gif vistavalid.zip (http://www.traidnt.net/vb/attachments/435999d1260831862-vistavalid.zip) (66.4 كيلوبايت, عدد مرات المشاهدة 765 مرة)


https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-ash4/482113_236967293114455_1193518507_n.png (http://www.dzbatna.com)
©المشاركات المنشورة تعبر عن وجهة نظر صاحبها فقط، ولا تُعبّر بأي شكل من الأشكال عن وجهة نظر إدارة المنتدى (http://www.dzbatna.com)©

استعمل مربع البحث في الاسفل لمزيد من المواضيع


سريع للبحث عن مواضيع في المنتدى