Skip to content

Instantly share code, notes, and snippets.

@dciccale
Forked from 140bytes/LICENSE.txt
Created August 2, 2011 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dciccale/1119280 to your computer and use it in GitHub Desktop.
Save dciccale/1119280 to your computer and use it in GitHub Desktop.
FormValidator.js - Form validation in 114 bytes of JavaScript
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>
@dciccale
Copy link
Author

updated, now 144 bytes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment