首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >验证不使用INTL-TEL-INPUT

验证不使用INTL-TEL-INPUT
EN

Stack Overflow用户
提问于 2015-04-27 10:47:40
回答 3查看 25.3K关注 0票数 6

我正在尝试使用intl-tel-input...https://github.com/Bluefieldscom/intl-tel-input#utilities-script

我已经在工作表的顶部包含了css

代码语言:javascript
复制
<link rel="stylesheet" href="css/intlTelInput.css">

我的表格在中间有"tel“id..。我还使用parsley来验证表单的某些部分,并且运行得很好,

代码语言:javascript
复制
<input class="form-control input-sm"  id="tel" name="tel" type="tel" data-parsley-required>

在我的页面底部,我包含了jquery、bootstrap等。和intl-tel-input js....

代码语言:javascript
复制
 <script src="js/jquery.js"></script>
 <script src="js/bootstrap.min.js"></script>
 <script src="js/parsley.js"></script>
 <script src="js/intlTelInput.min.js"></script>

然后我初始化这个实用程序。

代码语言:javascript
复制
$("#tel").intlTelInput({
            utilsScript: "js/utils.js"
});

表单元素拾取国家/地区标志,因此插件看起来正在工作,但没有进行验证。utils.js文件是该文件的“编译”版本,我认为它是要使用的正确版本--但我已经尝试了编译和非编译版本。

这里我漏掉了什么?为什么验证和格式化不起作用?

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2016-07-21 15:32:54

对于工作验证,您需要调用utils.js验证函数,请参见下面的示例

代码语言:javascript
复制
$("#tel").change(function()
{
  var telInput = $("#tel");
  if ($.trim(telInput.val())) 
  {
    if (telInput.intlTelInput("isValidNumber")) 
    {
      console.log(telInput.intlTelInput("getNumber"));
      alert('Valid' );
    }
    else 
    {
      console.log(telInput.intlTelInput("getValidationError"));
      alert('invalid');
    }
  }
});

票数 9
EN

Stack Overflow用户

发布于 2016-01-30 04:21:08

我在一个类似的演示中遇到了问题,因为它似乎加载了模块,但我没有得到输入的验证。

我发现我的问题是,当我试图访问正在使用intlTelInput.js的输入域时,它并没有完全加载。

我通过允许页面完全加载的方式对其进行了测试,验证工作与预期的一样。但是,每当我使用快速验证时,验证都不起作用。此外,即使输入字段有值,getNumber方法也会返回"“。

检查两件事: 1)使用字段时intlTelInput.min.js是否完全加载?如果没有,则延迟对该字段的访问,直到加载脚本为止。2) utilScript的路径是否正确?此库是验证所必需的。

顺便说一句,当验证代码正常工作时,字段内容将以您从“标志”下拉列表中选择的国家/地区的样式进行格式化。例如,当我选择"US“时,我的默认格式是(202) 555-1212

票数 1
EN

Stack Overflow用户

发布于 2015-06-30 16:34:59

http://js-tutorial.com/international-telephone-input-711这会对你有帮助的。

链接内容:

代码语言:javascript
复制
1. INCLUDE CSS AND JS FILES
<link rel="stylesheet" href="build/css/intlTelInput.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="build/js/intlTelInput.min.js"></script>
2. HTML
<input type="tel" id="mobile-number">
3. JAVASCRIPT
$("#mobile-number").intlTelInput();
4. OPTIONS
Note: any options that take country codes should be lower case ISO 3166-1 alpha-2 codes
autoHideDialCode: If there is just a dial code in the input: remove it on blur, and re-add it on focus. This is to prevent just a dial code getting submitted with the form.
Type: Boolean Default: true
defaultCountry: Set the default country by it's country code. Otherwise it will just be the first country in the list.
Type: String Default: ""
defaultStyling: The position of the selected flag: "inside" or "outside" (relative to the input).
Type: String Default: "inside"
dialCodeDelimiter: Choose the delimiter that is inserted after the dial code when the user selects a country from the dropdown.
Type: String Default: " "
nationalMode: Don't insert the international dial code when the user selects a country from the dropdown.
Type: Boolean Default: false
onlyCountries: Display only the countries you specify.
Type: Array Default: undefined
preferredCountries: Specify the countries to appear at the top of the list.
Type: Array Default: ["us", "gb"]
validationScript: Enable validation by specifying the URL to the included libphonenumber script. This ~200k script is fetched only when the page has finished loading (to prevent blocking), and is then accessible through the publicisValidNumber function.
Type: String Default: "" Example: "lib/libphonenumber/build/isValidNumber.js"
5. METHODS
destroy: Remove the plugin from the input, and unbind any event listeners
$("#mobile-number").intlTelInput("destroy");
getSelectedCountryData: Get the country data for the currently selected flag
$("#mobile-number").intlTelInput("getSelectedCountryData");
Returns something like this:
{
  name: "Afghanistan (‫افغانستان‬‎)",
  iso2: "af",
  dialCode: "93"
}
isValidNumber: Validate the current number using Google's libphonenumber (requires the validationScript option to be set correctly). Expects an internationally formatted number. Optionally pass the argument true to accept national numbers as well.
$("#mobile-number").intlTelInput("isValidNumber");
Returns: true/false
selectCountry: Select a country after initialisation (e.g. when the user is entering their address)
$("#mobile-number").intlTelInput("selectCountry", "gb");
setNumber: Insert a number, and update the selected flag accordingly
$("#mobile-number").intlTelInput("setNumber", "+44 77333 123 456");
6. STATIC METHODS
getCountryData: Get all of the plugin's country data
var countryData = $.fn.intlTelInput.getCountryData();
Returns an array of country objects:
[{
  name: "Afghanistan (‫افغانستان‬‎)",
  iso2: "af",
  dialCode: "93"
}, ...]
setCountryData: Set all of the plugin's country data
$.fn.intlTelInput.setCountryData(countryData);
7. VALIDATION
International number validation is hard (it varies by country/district). The only comprehensive solution I have found is Google's libphonenumber, which I have precompiled into a single JavaScript file and included in the lib directory. Unfortunately even after minification it is still ~200kb, so I have included it as an optional extra. If you specify the validationScript option then it will fetch the script only when the page has finished loading (to prevent blocking), and will then be accessible through the public isValidNumber function.
8. CSS
Image path: Depending on your project setup, you may need to override the path to flags.png in your CSS.
.intl-tel-input .flag {background-image: url("path/to/flags.png");}
Input margin: For the sake of alignment, the default CSS forces the input's vertical margin to 0px. If you want vertical margin, you should add it to the container (with class intl-tel-input).
Displaying error messages: If your error handling code inserts an error message before the <input> it will break the layout. Instead you must insert it before the container (with class intl-tel-input).
Share
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29886381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档