javascript 验证不适用于 INTL-TEL-INPUT
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29886381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Validation no working with INTL-TEL-INPUT
提问by Skinner
I'm trying to use intl-tel-input... https://github.com/Bluefieldscom/intl-tel-input#utilities-script
我正在尝试使用 intl-tel-input ... https://github.com/Bluefieldscom/intl-tel-input#utilities-script
I've included the css at the top of the sheet
我已经在工作表的顶部包含了 css
<link rel="stylesheet" href="css/intlTelInput.css">
My form in the middle with "tel" id... I am also using parsley to validate parts of the form, and that is working fine,
我的表格中间带有“电话”ID...我还使用欧芹来验证表格的一部分,并且工作正常,
<input class="form-control input-sm" id="tel" name="tel" type="tel" data-parsley-required>
and at the bottom of my page i included the jquery, bootstrap, etc... and intl-tel-input js....
在我的页面底部,我包含了 jquery、bootstrap 等……以及 intl-tel-input js……
<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>
And then i initialize the utility...
然后我初始化该实用程序...
$("#tel").intlTelInput({
utilsScript: "js/utils.js"
});
The form element picks up the country flags, so the plug in appears to be working, but no validation is occurring. The utils.js file is the "compiled" version of the file, which i believe is the correct one to use - but i've tried both the compiled and non compiled version.
表单元素获取国家/地区标志,因此插件似乎正在运行,但没有进行验证。utils.js 文件是该文件的“编译”版本,我认为这是正确的使用 - 但我已经尝试了编译和非编译版本。
What am i missing here? why isn't the validation and formatting working?
我在这里错过了什么?为什么验证和格式不起作用?
thanks.
谢谢。
回答by Prathmesh
For working validation you need call utils.js validation functions See below example
对于工作验证,您需要调用 utils.js 验证函数 请参见下面的示例
$("#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');
}
}
});
回答by Mark West
I had problem with a similar presentation, in that it appeared to load the module but I didn't get validation of the input.
我在类似的演示中遇到了问题,因为它似乎加载了模块,但我没有得到输入的验证。
I found my problem was that the intlTelInput.js wasn't fully loaded when I tried to access the input field that was using it.
我发现我的问题是当我尝试访问正在使用它的输入字段时 intlTelInput.js 没有完全加载。
I tested it by allowing the page to fully load, and the validation worked as expected. Any time I used the quickly, however, validation didn't work. Further, the getNumber method would also return "" even if the input field had a value.
我通过允许页面完全加载来测试它,并且验证按预期工作。但是,任何时候我快速使用它时,验证都不起作用。此外,即使输入字段有值,getNumber 方法也会返回 ""。
Check two things: 1) Is intlTelInput.min.js fully loaded when you use the field? If not, delay access to the field until the script is loaded. 2) Is the path for utilScript correct? This library is required for validation.
检查两件事:1) 使用该字段时 intlTelInput.min.js 是否已完全加载?如果没有,请延迟对该字段的访问,直到加载脚本。2) utilScript 的路径是否正确?验证需要此库。
BTW, when the validation code is working properly, the field contents will format in the style of the country you have selected from the 'flags' dropdown. For example, when I select "US" my default format is (202) 555-1212
顺便说一句,当验证码正常工作时,字段内容将按照您从“标志”下拉列表中选择的国家/地区的样式进行格式化。例如,当我选择“美国”时,我的默认格式是 (202) 555-1212
回答by Santosh Anantharamaiah
http://js-tutorial.com/international-telephone-input-711this would be helpful for you.
http://js-tutorial.com/international-telephone-input-711这对你有帮助。
Content of the link:
链接内容:
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