Created
January 23, 2012 03:05
-
-
Save eduardolundgren/1660210 to your computer and use it in GitHub Desktop.
Alloy Form Validate nonZeroFloat, nonZeroInt
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> | |
<html> | |
<head> | |
<script src="http://deploy.alloyui.com/build/aui/aui.js" type="text/javascript"></script> | |
<link rel="stylesheet" href="http://deploy.alloyui.com/build/aui-skin-classic/css/aui-skin-classic-all-min.css" type="text/css" media="screen" /> | |
<style type="text/css"> | |
.aui-form-validator-stack-error { | |
color: red; | |
display: block; | |
font-weight: normal; | |
font-style: italic; | |
margin: 3px 0; | |
} | |
.aui-form-validator-error { | |
background: lightPink; | |
} | |
.aui-form-validator-valid { | |
background: lightGreen; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Alloy - FormValidator</h1> | |
<form action="" id="fm1" name="fm1"> | |
<input class="aui-field-required aui-field-nonZeroFloat" type="number" name="fValue1" value="1.0" step="0.1" /> | |
<input class="aui-field-required aui-field-nonZeroInt" type="number" name="iValue1" value="5" /> | |
<input class="aui-field-required aui-field-nonZeroInt" type="number" name="iValue2" value="1000" /> | |
</form> | |
<script type="text/javascript"> | |
AUI().ready('aui-form-validator', 'aui-overlay-context-panel', function(A) { | |
A.mix( | |
YUI.AUI.defaults.FormValidator.STRINGS, | |
{ | |
nonZeroFloat: 'This field should be a float number different from zero.', | |
nonZeroInt: 'This field should be a integer number different from zero.' | |
}, | |
true | |
); | |
var isFloat = function (n) { | |
return (+n === n) && !!(n % 1); | |
}; | |
var isInt = function (n) { | |
return (n === ~~n); | |
}; | |
A.mix( | |
YUI.AUI.defaults.FormValidator.RULES, | |
{ | |
nonZeroFloat: function(val, fieldNode, ruleValue) { | |
var fVal = Number(val); | |
return isFloat(fVal) && (fVal != 0); | |
}, | |
nonZeroInt: function(val, fieldNode, ruleValue) { | |
var iVal = Number(val); | |
return isInt(iVal) && (iVal != 0); | |
} | |
}, | |
true | |
); | |
var validator1 = new A.FormValidator({ | |
boundingBox: '#fm1' | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment