javascript how to spell the function properly
-
The picture is:
var validation = $('#check').on('click', function() {
function init() { function checkname() { var nameL = $("#name").val().length; var nameVal = $('#name').val(); var regexp = /^[a-zA-Zа-яА-ЯёЁіІїЇҐґ'][a-zA-Z-а-яА-ЯёЁіІїЇҐґ']+[a-zA-Zа-яА-ЯёЁіІїЇҐґ']?\s[a-zA-Zа-яА-ЯёЁіІїЇҐґ'][a-zA-Z-а-яА-ЯёЁіІїЇҐґ']+[a-zA-Zа-яА-ЯёЁіІїЇҐґ']?$/; if (nameL === 0) { alert('Будь ласка введіть ім\'я та прізвище') } else if (nameVal.search(regexp)) { alert('перевірте ім\'я та прізвище') } }; //end func checkname return { checkname: checkname }; }; // end func init return { init: init };
});
It's a file I'm training in cats, a chequenake function that doesn't work well. Actually, there's three of them. I know they're not a traw, I'll take them out later. I don't understand how I can get these functions out of here so they can work, that's when the button is pressed into three fields.
-
Method
.on
As an argument, it takes a function (i.e.callback
(in this case, in the event)click
)It will not be possible to remove the restorative function as this function is (i)
callback
to be codedjQuery
And you don't have access.To check the 3 field on the application
#check
for example:function checkname() { var nameL = $("#name").val().length; var nameVal = $('#name').val(); var regexp = /^[a-zA-Zа-яА-ЯёЁіІїЇҐґ'][a-zA-Z-а-яА-ЯёЁіІїЇҐґ']+[a-zA-Zа-яА-ЯёЁіІїЇҐґ']?\s[a-zA-Zа-яА-ЯёЁіІїЇҐґ'][a-zA-Z-а-яА-ЯёЁіІїЇҐґ']+[a-zA-Zа-яА-ЯёЁіІїЇҐґ']?$/; if (nameL === 0) { alert('Будь ласка введіть ім\'я та прізвище') } else if (nameVal.search(regexp)) { alert('перевірте ім\'я та прізвище') } }
function checkmail() {
...
}function checkpass() {
...
}$('#check').on('click', function() {
checkname();
checkmail();
checkpass();
});
UPD
If it is necessary that these functions be isolated from the global area of the name (or from any other), the simplest way will be to lock them in the bulbeck:
$('#check').on('click', function() {
!function checkname() {...}(); //имя функции указывать необязательно
(function checkmail() {...})(); //имя функции указывать необязательно
(function checkpass() {...}()); //имя функции указывать необязательно
});
Another option is to create an object that will contain these three functions as methods. The facility will be accessible globaland functions will be available only through this facility:
var validation = {
checkname: function() {...},
checkmail: function() {...},
checkpass: function() {...}
}$('#check').on('click', function() {
validation.checkname();
validation.checkmail();
validation.checkpass();
});