做开发这么久一直被表单验证这个看似简单又不简单的东西搞得非常烦躁,于是写了个表单验证的框架,再也不用担心这个心烦的验证了。
这里使用AngularJS的指令进行处理代码及其简洁明了
下面是指令JS代码
app.directive('ccForm',['$parse',function ($parse) {
return {
restrict:'A',
link:function (scope,element,attrs) {
var first=true;
var errors=0;
var checkInterval;
function showError(input,errorIndex) {
if(first){
errors++;
$parse(element.attr('cc-form')).assign(scope,false);
refreshScope(scope);
return;
}
input.addClass('hasError');
input.closest('.form-group').addClass('hasError');
input.next('.help-block').find('.cc-show').removeClass('cc-show');
input.next('.help-block').find('[cc-error-'+errorIndex+']').addClass('cc-show');
input.closest('.form-group').next('.help-block').find('.cc-show').removeClass('cc-show');
input.closest('.form-group').next('.help-block').find('[cc-error-'+errorIndex+']').addClass('cc-show');
}
function hidError(input,errorIndex) {
errors--;
if(errors==0){
$parse(element.attr('cc-form')).assign(scope,true);
refreshScope(scope);
}
input.removeClass('hasError');
input.closest('.form-group').removeClass('hasError');
input.next('.help-block').find('[cc-error-'+errorIndex+']').removeClass('cc-show');
input.closest('.form-group').next('.help-block').find('[cc-error-'+errorIndex+']').removeClass('cc-show');
}
function checkInput(input) {
var that=$(input);
// $('[cc-email]')[0].attributes[0].name
var attrs=input.attributes;
var value=that.val();
for(var i=0,attr;attr=attrs[i];i++){
if(attr.name=='cc-email'){
if(/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(value)){
hidError(that,attr.value)
}else{
showError(that,attr.value);
break;
}
}else if(attr.name=='cc-phone'){
if(/\d{11}/.test(value)){
hidError(that,attr.value);
}else {
showError(that,attr.value);
break;
}
}else if(attr.name=='cc-max'){
if(value.length>attr.value){
showError(that,that.attr(attr.name+'-error'));
break;
}else {
hidError(that,that.attr(attr.name+'-error'));
}
}else if(attr.name=='cc-min'){
if(value.length<attr.value><p> </p>
<p></p>
<p>对应的需要一点点CSS代码,下面是用LESS写的</p>
<pre class="brush:js;toolbar:false">.help-block {
* {
&:not(.cc-show) {
display: none;
}
}
}
对应的CSS就是
.help-block *:not(.cc-show) {
display: none;
}
用法1
<div> <h1>test</h1> <form> <div> <input> <div> <p>密码长度不能超过16位</p> <p>密码最短为6</p> </div> </div> <div> <input> <div> <p>请输入正确的手机号</p> </div> </div> <div> <input> <div> <p>请输入传真</p><div class="aritcle_card flexRow artxards"> <div class="artcardd flexRow"> <a class="aritcle_card_img" rel="nofollow" href="/ai/4159" title="序列猴子"><img src="https://img.php.cn/upload/ai_manual/001/246/273/178599610046869.png" alt="序列猴子" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a> <div class="aritcle_card_info flexColumn"> <a rel="nofollow" href="/ai/4159" title="序列猴子" class="overflowclass">序列猴子</a> <p class="overflowclass">序列猴子是一款AI大模型工具,出门问问推出的一款超大规模的语言模型。</p> </div> <a rel="nofollow" href="/ai/4159" title="序列猴子" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a> </div> </div> </div> </div> <div> <input> <div> <p>请输入正确的邮箱</p> </div> </div> <button>提交</button> </form> </div>
用法2
如此简洁明了










