-
-
Save dciccale/1119280 to your computer and use it in GitHub Desktop.
FormValidator.js - Form validation in 114 bytes of JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function( | |
a, // elements | |
b, // elements length | |
c // each dom element and then its value | |
) { | |
// check if element is an array of elements | |
a=a.type ? [a] : a; | |
for(b=a.length;b--&&!c;) { | |
// save reference to the element | |
c=a[b] | |
// get the value of the form element | |
c=/di|b/.test(c.type) ? c.checked : !!c.value | |
} | |
// switch the bool value | |
return!!c | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(b,c,a){b=b.type?[b]:b;for(c=b.length;c--&&!a;)a=b[c],a=/di|b/.test(a.type)?a.checked:!!a.value;return!!a} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Denis Ciccale <http://webdecs.wordpress.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "FormValidator", | |
"description": "Validate any form field", | |
"keywords": [ | |
"field", | |
"validation", | |
"form", | |
"validator" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<title>FormValidator</title> | |
<script> | |
var validate = function(b,c,a){b=b.type?[b]:b;for(c=b.length;c--&&!a;)a=b[c],a=/di|b/.test(a.type)?a.checked:!!a.value;return!!a}; | |
</script> | |
<p> | |
<input type="text" id="text" /> | |
<a href="#" onclick="alert(validate(document.getElementById('text')));return false">has value?</a> | |
</p> | |
<p> | |
<select id="select"> | |
<option value=""></option> | |
<option value="option_1">Option 1</option> | |
<option value="option_2">Option 2</option> | |
</select> | |
<a href="javascript:alert(validate(document.getElementById('select')))">has value?</a> | |
</p> | |
<p> | |
<textarea id="textarea" rows="4" cols="50"></textarea> | |
<a href="javascript:alert(validate(document.getElementById('textarea')))">has value?</a> | |
</p> | |
<p> | |
<input type="password" id="password" /> | |
<a href="javascript:alert(validate(document.getElementById('password')))">password has value?</a> | |
</p> | |
<p> | |
<input type="checkbox" id="checkbox" /> | |
<a href="javascript:alert(validate(document.getElementById('checkbox')))">is checked?</a> | |
</p> | |
<p> | |
<input type="radio" name="radios" id="radio1" /> | |
<input type="radio" name="radios" id="radio2" /> | |
<input type="radio" name="radios" id="radio3" /> | |
<input type="radio" name="radios" id="radio4" /> | |
<a href="#" onclick="alert(validate(document.querySelectorAll('input[type=radio]')));return false">is any radio checked?</a> | |
</p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
updated, now 144 bytes