-
-
Save dciccale/1119280 to your computer and use it in GitHub Desktop.
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 | |
} |
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} |
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. |
{ | |
"name": "FormValidator", | |
"description": "Validate any form field", | |
"keywords": [ | |
"field", | |
"validation", | |
"form", | |
"validator" | |
] | |
} |
<!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> |
r is in reset, a and d in password, i in hidden, and o in password again...
so /ra|x/.test(c)
would work
thaks, but.. reset is not a field, also
i don't think a hidden field needs to be validated for empty if a user can't type a value,
and x is in textarea.
i will use /d.|b/
textarea is a tag, not a type, but reset is; if you use form.getElementsByTagName('*') instead of form.elements, you'll have errors.
anyway i am testing the type, and textarea does have a "textarea" type so is good for this case.
this code is not for a full form validation, as you can see it can validate a group of elements, but exiting the for loop if just one have a value, so as i wrote in the anotated, validating a collection of elements may be only useful for radio groups, where you can only choose one of the group. anyway you could execute this validation function for each form element you want to validate for empty value.
updated, now 144 bytes
I think there is a problem with /d|b/.test(c) when testing the type passworD.
Maybe you can pick another letter to check.