V
V
Valeriy19972015-08-26 09:55:44
JavaScript
Valeriy1997, 2015-08-26 09:55:44

How to implement text field validation?

There is a text field:

<input  id="phone" type="text" autocomplete="off" class="telephone"   value="Введите Ваш номер" name="input" >


and there is a button:
<button id="enter" class="entr btn" disabled  type="submit">Enter</button>


you need to check for the number of digits: no more than 12, and for compliance with the mob. operators, and until the validation conditions are satisfied, the submit button will be in the disabled state. Upon successful input, the disabled state is removed, and the data about the time and the entered number are saved to a variable in json format.

I think to implement it like this:

function checkParams() {
    var phone = $('#phone').val();
    
    if(phone.length !== 12 ) {
    alert("ne12")
        $('#enter').removeAttr('disabled');
    } else {
    alert("12")
        $('#enter').attr('disabled', 'disabled');
    }
}


but I don’t know how to check for operators (
maybe I get a string from an input into a variable, I throw the first digits into an array, and compare it with another array where these numbers are? If the check was successful, I copy the number completely to another array. And how to copy it along with the date
And most importantly, on what event should these checks be done? (originally, I planned on the event :.on('input', function () {

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-08-26
@Valeriy1997

In theory, you need to climb to the server, compare it with the database of mobile numbers and return the desired operator. It's in theory. In practice - On the one hand, everything is decided by the operator code in 3 digits and the base of the Ministry of Communications describing which numbers were issued to which operator, and on the other hand, we have the opportunity to change the operator without changing the phone number (and if you do not have access to up-to-date data - checking the operator code will cause an error), and we also have such an operator as "Hello incognito", whose subscribers may look like a Moscow landline phone or MTS / Megafon / Beeline, but not be them. In my opinion, you need to collect a small database of 4-digit numbers (operator code + 1 first digit of the number) Based on these data, find out if you belong to a certain operator according to the Ministry of Communications, offer the user this operator,
PS do not forget that Javascript can be disabled.

S
Sergey Zelensky, 2015-08-26
@SergeyZelensky-Rostov

no input event there is an onchange event

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question