Jump to content

[SOLVED] php forms - Picky text inputs...?


ag3nt42

Recommended Posts

Hello again all,

 

I find myself in a situation where I can't have the user put in wrong information or the process I have going will screw up.. And There is to much variation in the input to restrict it to a selection box. or the like...

 

I want to be able to tell the text input which characters its allowed to accept and in what format...

any one have ne ideas on this one? ???

 

~thanks all.

Link to comment
https://forums.phpfreaks.com/topic/107876-solved-php-forms-picky-text-inputs/
Share on other sites

The following will only accept numbers and letters:

 

$str = 'This is a string with the number 01.';
$isGood = FALSE;
foreach(str_split($str) as $val){
if(ctype_alnum($val)){
	$isGood = TRUE;
}else{
	$isGood = FALSE;
}
}

if($isGood){
echo "String is valid.";
}else{
echo "String is not valid.";
}

if(!ereg("^[0-9]+$", $_POST['input']) || strlen($_POST['input']) > 4 || strlen($_POST['input']) < 4)
{
   die("error");
}
else
{
   echo "success";
}

 

precisely what I was looking for.. thanks you much haku..

 

i only had to alter it alil bit..

 

///////////////////////////
/// Validate Year Input ///
///////////////////////////
if (!(isset($_GET['year'])))
{
echo("");
}
else
{
if(!ereg("^[0-9]+$", $_GET['year']) || strlen($_GET['year']) > 4 || strlen($_GET['year']) < 4)
{
   die("<script>alert(VAL);</script>");
}
else
{
   	 	echo "success";
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.