<?php
if($chaine contient autre que"..."){echo'erreur';}
?>
[Note : ce message a été posté de manière anonyme avant d'être réattribué à son auteur]
Ca devrait quand même te donner une idée de comment définir une liste de caractères autorisés, et spécifié que tu souhaite trouver tout caractère qui ne se trouve pas dans la liste de caractères autorisés[a-p] : défini tout alphabétique compris entre a et p.
[^a-p] : tout caractère qui n'est pas compris dans la fourchette a-p
[a-p0-6] : toutes les lettres de a à p et chiffres de 0 à 6
<?php
$chaine = "";
if(preg_match('[^a-z]', $chaine) AND preg_match('[^A-Z]', $chaine) AND preg_match('[^0-9]', $chaine))
{
echo"";
}
?>
SI ce code est bon :<?php
$chaine = "bonjour";
if(preg_match('[^a-z]', $chaine) AND preg_match('[^A-Z]', $chaine) AND preg_match('[^0-9]', $chaine))
{
echo"La chaine : $chaine contient autre chose que abcdefghijklmnop, que ABCDEFGHIJKLMNOP ou que 0123456789";
}
?>
Comment faire pour mettre dans $faux_caracteres les caractères autre que ceux préciser, c'est juste pour dire dans le message d'erreur > il y a un @ dans votre chaine veuillez le retier.<?php
$chaine = "bonjo@ur";
if(preg_match('[^a-z]', $chaine) AND preg_match('[^A-Z]', $chaine) AND preg_match('[^0-9]', $chaine))
// $faux_caracteres = "@"; car il y a un arobase dans bonjour
{
echo"La chaine : $chaine contient un $faux_caracteres veuillez le retirer";
}
?>
Bon je vien d'expédier co code sur le serveur, et sa ne marche pas comme je le veut.
$chaine = "Bonjo@ur";
$masque = "/([^a-zA-Z0-9])/";
if (preg_match($masque, $chaine, $matches)) {
print_r($matches); // affiche le tableau contenant le caractère erronné
echo "Erreur : " . $matches[1];
}
Tu peux également utiliser preg_match_all() si tu veux récupérer dans le tableau tous les caractères qui ne respectent pas le masque $masque_identifiant = "/([^a-zA-Z0-9])/";
if (preg_match($masque_identifiant, $identifiant, $matches_identifiant))
{
print_r($matches_identifiant);
if($matches_identifiant[1] != "") {echo "Votre identifiant contient $matches_identifiant[1]";}
if($matches_identifiant[2] != "") {echo "Votre identifiant contient $matches_identifiant[2]";}
if($matches_identifiant[3] != "") {echo "Votre identifiant contient $matches_identifiant[3]";}
if($matches_identifiant[4] != "") {echo "Votre identifiant contient $matches_identifiant[4]";}
if($matches_identifiant[5] != "") {echo "Votre identifiant contient $matches_identifiant[5]";}
if($matches_identifiant[6] != "") {echo "Votre identifiant contient $matches_identifiant[6]";}
if($matches_identifiant[7] != "") {echo "Votre identifiant contient $matches_identifiant[7]";}
if($matches_identifiant[8] != "") {echo "Votre identifiant contient $matches_identifiant[8]";}
if($matches_identifiant[9] != "") {echo "Votre identifiant contient $matches_identifiant[9]";}
if($matches_identifiant[10] != "") {echo "Votre identifiant contient $matches_identifiant[10]";}
if($matches_identifiant[11] != "") {echo "Votre identifiant contient $matches_identifiant[11]";}
if($matches_identifiant[12] != "") {echo "Votre identifiant contient $matches_identifiant[12]";}
if($matches_identifiant[13] != "") {echo "Votre identifiant contient $matches_identifiant[13]";}
if($matches_identifiant[14] != "") {echo "Votre identifiant contient $matches_identifiant[14]";}
if($matches_identifiant[15] != "") {echo "Votre identifiant contient $matches_identifiant[15]";}
if($matches_identifiant[16] != "") {echo "Votre identifiant contient $matches_identifiant[16]";}
if($matches_identifiant[17] != "") {echo "Votre identifiant contient $matches_identifiant[17]";}
if($matches_identifiant[18] != "") {echo "Votre identifiant contient $matches_identifiant[18]";}
if($matches_identifiant[19] != "") {echo "Votre identifiant contient $matches_identifiant[19]";}
if($matches_identifiant[20] != "") {echo "Votre identifiant contient $matches_identifiant[20]";}
}
Est-ce bon ?
$masque_identifiant = "/([^a-zA-Z0-9])/";
if (preg_match($masque_identifiant, $identifiant, $matches_identifiant))
{
print_r($matches_identifiant);
foreach($matches_identifiant as $key => $val) {
if ($val != "")
echo "Votre identifiant contient ".$matches_identifiant[$key];
}
}
mais il y a moyen de mieux penser le bazar.
Non ce n'est pas bon, car tu ne sais pas à l'avance combien de caractères erronnés l'utilisateur va saisir. Il faut donc parcourir le tableau comme le suggère Berzemus, le foreach est pas nécessairement judicieux puisque l'index "0" est particulier, mais sur le principe il vaut mieux utiliser une boucle qui s'arrêtera au nombre de caractères érronés.Est-ce bon ?
Ils sont déjà interdits, puisqu'ils ne font pas partie de la liste des caractères autorisés dans to masqueEt comment je fait si je veut interdire des signes comme - et _ ou @ ?
<?php
$identifiant = "AVION-NOIR_BLEU";
$masque_identifiant = "/([^a-zA-Z0-9\-\_])/";
preg_match($masque_identifiant, $identifiant, $matches_identifiant);
foreach($matches_identifiant as $key_identifiant => $val_identifiant)
{}
if ($val_identifiant != "")
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir de </font><font color="#336699" face="Verdana" size="5"><b>'.$matches_identifiant[$key_identifiant].'</b></font>';
}
?>
abcdefghijklmnopqrstuvwxyz et ABCDEFGHIJKLMNOPQRSTUVWXYZ et 0123456789 et _ et - sont autorisé !!Code : Tout sélectionner
Exemple : $identifiant = "Bienvenue@ Che@ " é";
Il y a 2 @, 1 # et 1 é
<?php
$identifiant = "AVION-NOIR_BLEU BLANC";
$masque_identifiant = "/([^a-zA-Z0-9\-\_])/";
preg_match($masque_identifiant, $identifiant, $matches_identifiant);
foreach($matches_identifiant as $key_identifiant => $val_identifiant)
{}
if(ereg(" ","$identifiant") AND !ereg("\ ","$masque_identifiant"))
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir d\' espace</font>';
}
elseif ($val_identifiant != "")
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir de </font><font color="#336699" face="Verdana" size="5"><b>'.$matches_identifiant[$key_identifiant].'</b></font>';
}
?>
J'ai même mieu fait :$masque_identifiant = "/([^a-zA-Z0-9\-\_\ ])/"; // abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _-0123456789
preg_match($masque_identifiant, $identifiant, $matches_identifiant);
foreach($matches_identifiant as $key_identifiant => $val_identifiant)
{}
if(ereg(" ","$identifiant") AND !ereg("\ ","$masque_identifiant"))
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir d\' espace</font>';
}
elseif(ereg(" ","$identifiant") AND ereg("\ ","$masque_identifiant"))
{
if ($val_identifiant != "")
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir de </font><font color="#336699" face="Verdana" size="5"><b>'.$matches_identifiant[$key_identifiant].'</b></font>';
}
}
elseif(!ereg(" ","$identifiant"))
{
if ($val_identifiant != "")
{
echo'<font color="#336699" face="Verdana">Le champs identifiant a pour valeur <input type="text" name="identifiant" size="15" maxlength="10" style="border: 1px solid #336699; font-weight:bold; color:#336699; text-align:center; font-family:Verdana; font-size:10pt" onFocus="javascript: this.blur()" value="'.$identifiant.'">, et ne devrait pas contenir de </font><font color="#336699" face="Verdana" size="5"><b>'.$matches_identifiant[$key_identifiant].'</b></font>';
}
}