Home » Jquery » Jquery validate text lentgh : javascript say ok maxlength <= 300 but php count 306-Exceptionshub

Jquery validate text lentgh : javascript say ok maxlength <= 300 but php count 306-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I don’t understand why javascript an php aren’t agree…

Here is a sample text :

Bonjour,
Je suis ravi de soumettre mon problème à la communauté stackoverflow - franchement c'est la meilleure du web pour les questions de codage .
Ce texte est fictif mais illustre bien ce que mes visiteurs parfois postent sur mon formulaire de contact en termes de longueur et contenu.
Merci salut

So, if I use in rules (jquery validate) that code:

demande: {
required: true,
rangelength: [5, 300],
regex: /^[a-zA-Z0-9 \'\’\.\,\;\!\?\:@_°àâéèëêîïôùûçÀÂÇÈÉÊËÏÎÔÙÛ\s\S\(\)\/\-]*$/
}

or this one:

demande: {
required: true,
regex: /^[a-zA-Z0-9 \'\’\.\,\;\!\?\:@_°àâéèëêîïôùûçÀÂÇÈÉÊËÏÎÔÙÛ\s\S\(\)\/\-]{5,300}$/
}

…in both cases jquery reports nothing (implied: text is no more than 300 characters), but then if I test $_POST[‘demande’] with PHP:

echo 'count text = '.strlen($_POST['demande']);

…PHP say 306 characters! 😮

And If I use this code:

if(preg_match("#^[a-zA-Z0-9 \'\’\.\,\;\!\?\:@_°àâéèëêîïôùûçÀÂÇÈÉÊËÏÎÔÙÛ\s\S\(\)\/\-]{5,300}$#", $_POST['demande'])){
echo 'good';
} else { echo 'wrong'; }

…PHP say ‘wrong’!

So why do you think PHP is not agree?

How to&Answer: