确定表单输入是否具有焦点 [英] Determine whether form input has focus

查看:201
本文介绍了确定表单输入是否具有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的AngularJS验证,我有,如果有3种类型的错误时显示一个div。

I'm doing validation in AngularJS, and I have a div that is displayed if there are 3 types of errors.

有关要求我要显示错误消息仅在页与空值提交

For required I want to show the error message only if the page is submitted with empty value

<div class="error" data-ng-show="submitted && mainForm.email.$error.required" />

有关正则表达式验证我希望它标志实时,默认的行为。

For the regex validation I want it to flag real-time, default behavior.

<div class="error" data-ng-show="mainForm.email.$error.pattern" />

我现在面临的问题是与MINLENGTH个。我不想表现出来,而他们正在打字。这很烦人,因为他们没有完成打字。我不希望显示它提交要么,我认为这是为时已晚。我想表明,当他们不再在元素的焦点。

The problem I'm facing is with minlength. I don't want to show it while they are typing. It's annoying because they haven't finished typing. I don't want to display it on submit either, I think that's too late. I'd like to show it when they are no longer in focus of the element.

如果//mainForm.email.$focus存在我可以简单地做到这一点。

If //mainForm.email.$focus existed I could simply do this

<div class="error" data-ng-show="mainForm.email.$error.minlength && 
!mainForm.email.$focus"/>

任何人都知道的任何方式做这样的检查或任何非抽出替代的?

Anyone know of any way to do this kind of check or any non drawn out alternative?

谢谢!

推荐答案

是它结果
您可以使用ngFocus和ngBlur为此

Yes it does
you can use ngFocus and ngBlur for this purpose

这里是code

<form name="mainForm">
    <span ng-show="mainForm.email.$error.pattern && !focus">error here</span>
    <input name="email" ng-pattern="..." ng-focus="focus=true" ng-blur="focus=false" type="text" />
</form>

ngFocus和ngBlur采取前pression和ngShow显示在真实评价

ngFocus and ngBlur take expression and ngShow shows upon true evaluation

这篇关于确定表单输入是否具有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆