2015-04-27 13 views
5

Sto cercando di utilizzare intl-tel-ingresso ... https://github.com/Bluefieldscom/intl-tel-input#utilities-scriptvalidazione non si lavora con INTL-TEL-INPUT

Ho incluso il CSS nella parte superiore del foglio

<link rel="stylesheet" href="css/intlTelInput.css"> 

la mia forma in mezzo con id "tel" ... inoltre sto usando il prezzemolo per convalidare parti del modulo, e che sta lavorando bene,

<input class="form-control input-sm" id="tel" name="tel" type="tel" data-parsley-required> 

e in fondo alla mia pagina ho inserito il jquery, bootstrap, ecc ... e 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> 

e poi ho inizializzare l'utilità ...

$("#tel").intlTelInput({ 
      utilsScript: "js/utils.js" 
}); 

L'elemento modulo raccoglie le bandiere del paese, in modo che il plug-in sembra funzionare, ma senza la convalida è in corso. Il file utils.js è la versione "compilata" del file, che credo sia quella corretta da utilizzare, ma ho provato sia la versione compilata che non compilata.

Cosa mi manca qui? perché la validazione e la formattazione non funzionano?

grazie.

risposta

-1

http://js-tutorial.com/international-telephone-input-711 questo sarebbe utile per voi.

Contenuto del 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 
+1

Si prega di aggiungere informazioni dal link alla risposta in quanto i collegamenti possono morire. – JackWhiteIII

+0

Ciao Jack, informazioni aggiunte. –

+0

@ Mark West: si prega di accettare la risposta, se ha risolto il problema. –

0

ho avuto un problema con una presentazione simile, nel senso che è apparso per caricare il modulo, ma non ho avuto la validazione dell'input.

Ho scoperto che il mio problema era che intlTelInput.js non era completamente caricato quando ho provato ad accedere al campo di input che lo stava usando.

L'ho testato consentendo il caricamento completo della pagina e la convalida ha funzionato come previsto. Ogni volta che ho usato rapidamente, tuttavia, la convalida non ha funzionato. Inoltre, il metodo getNumber restituirà anche "" anche se il campo di input ha un valore.

Controllare due cose: 1) È intlTelInput.min.js completamente caricato quando si utilizza il campo? In caso contrario, ritardare l'accesso al campo fino a quando lo script non viene caricato. 2) Il percorso per utilScript è corretto? Questa libreria è richiesta per la convalida.

BTW, quando il codice di convalida funziona correttamente, il contenuto del campo verrà formattato nello stile del paese selezionato dal menu a discesa "flags". Ad esempio, quando si seleziona "USA" il mio formato di default è (202) 555-1212

+0

Sono nuovo di questo.Qualcuno può aiutarmi a capire perché questo è stato downvoted? –

+0

Non sono sicuro di @Mark, ma poiché ho pensato che la tua risposta fosse utile, ho dato un +1 – osullic

+0

Ho una validazione funzionante, ma i contenuti del campo non sono formattati nello stile del paese selezionato. Qualche idea sul perché sarebbe? –

2

Per la convalida di lavoro è necessario chiamare utils.js funzioni di validazione Vedi sotto esempio

$("#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'); 
 
    } 
 
    } 
 
});

Problemi correlati