1
I have the following function in javaScript
:
function teste(ctrl) {
var str = ctrl.value;
var str = str.split(" ");
if (str.every(isBigEnough) && str.length >= 2) {
alert(true);
} else {
alert(false);
}
}
function isBigEnough(element, index, array) {
return element.length > 1;
}
<input id="nome" name="nome" type="text" placeholder="Nome completo" required onchange="teste(this)">
I want to replicate it in php
to have this validation also in the back-end
There is a similar php function, the array_walk
I’m just not quite there yet.
It would be something like:
function isBigEnough($item, $key) {
return strlen($item) >= 2;
}
function validaNome($value) {
var $str = $value;
var $str = explode(" ",$value);
if (array_walk($str, 'isBigEnough') && count($str) >= 2) {
return true;
}else{
return false;
}
}
needs to be similar to javascript version technique?
– Daniel Omine
needs to perform the same action, in
javascript
I blow up thestring
and I check the size of each element of it, if each element is larger than 2 ok, otherwise it does not pass, I wanted to avoid theforeach
, but if it’s the least painful way, okay– MarceloBoni
Okay, what matters is the end result.
– Daniel Omine