Jump to content

[SOLVED] Removing Special Characters


monkeybidz

Recommended Posts

I have a form that has a text box:

 

<input name="weight" type="text" id="weight" size="10" maxlength="5">

 

When to form is submitted, how do i make it to where the users cannot add any special characters like a comma for instance. This field is for a weight result value, but i need it to be all numeric at result. How can i strip it or make it return an erro on submit.

 

Currently my error system is like so:

 

if($weight ==    ) {  //This is where i want to add the check for special characters//

    $msg[] = $error7;

    header("Location: quote_error.php?");

}

Link to comment
https://forums.phpfreaks.com/topic/70847-solved-removing-special-characters/
Share on other sites

Hello,

 

You can do this by using preg_match. See the given example for reference.

 

Lets us consider the form is posted with a filled textbox having name as 'txtName'

		$result = preg_match("/^(\d+)+$/", $_POST['txtName']);

		// Executes if match found
		if ($result !=0)
		{
			echo "<center><font color='#FF0000'>Entered number is valid number :: </font>".$_POST['txtName']."</center><br>";
		}
		// else execute
		else
		{
			echo "<center><font color='#FF0000'>Entered text is invalid :: </font>".$_POST['txtName']."</center><br>";
		}

 

Hope this might solve your issue.

 

Regards,

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.