al3x8730 Posted September 7, 2008 Share Posted September 7, 2008 How do I disallow characters from being entered into a form? Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/ Share on other sites More sharing options...
Andy-H Posted September 7, 2008 Share Posted September 7, 2008 You need to use javascript / regex I think. E.g. <head> <script language="javascript"> function isEmail(str){ var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(str) == false) return false; return true; } </script> </head> <body> <input type="text" name="email" id="form_email" value="[email protected]" onFocus="if (this.value == '[email protected]'){ this.value = ''; }" onBlur="if ( isEmail(this.value) == false ){ this.value = '[email protected]'; alert('The email address entered is not a valid format.'); }" /> </body> Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636012 Share on other sites More sharing options...
Andy-H Posted September 7, 2008 Share Posted September 7, 2008 Thats to stop them typing in, you will also need to check with PHP incase they disable javascript. (or their browser doesnt support it) Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636013 Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Share Posted September 7, 2008 PHP check: <?php if (strlen($_POST['text_field_name']) > 0) { echo "The field is NOT empty!"; } ?> Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636048 Share on other sites More sharing options...
cooldude832 Posted September 7, 2008 Share Posted September 7, 2008 [quote author=Andy17 link=topic=215498.msg984994#msg984994 date=1220811607] PHP check: [code]<?php if (strlen($_POST['text_field_name']) > 0) { echo "The field is NOT empty!"; } ?> That doesn't mean they didn't enter bad characters You need a regular expression to do this effectively. ?> [/code] Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636053 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Some quick ones for you: Only digits: if (!ctype_digit($field)) { echo "Only digits are allowed."; } Only letters: if (!ctype_alpha($field)) { echo "Only letters are allowed."; } Alphanumeric: if (!ctype_alnum($field)) { echo "Only alphanumeric entries are allowed."; } Spaces, word characters (a-z and the _), numbers: if (!preg_match('/^[ \w\d]+$/', $field)) { //you get it by now } Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636059 Share on other sites More sharing options...
al3x8730 Posted September 7, 2008 Author Share Posted September 7, 2008 Some quick ones for you: Only digits: if (!ctype_digit($field)) { echo "Only digits are allowed."; } Only letters: if (!ctype_alpha($field)) { echo "Only letters are allowed."; } Alphanumeric: if (!ctype_alnum($field)) { echo "Only alphanumeric entries are allowed."; } Spaces, word characters (a-z and the _), numbers: if (!preg_match('/^[ \w\d]+$/', $field)) { //you get it by now } If I use the one to only allow alpha, will it also disallow spaces? Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636077 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Yes. Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636094 Share on other sites More sharing options...
Andy17 Posted September 7, 2008 Share Posted September 7, 2008 [quote author=Andy17 link=topic=215498.msg984994#msg984994 date=1220811607] PHP check: [code]<?php if (strlen($_POST['text_field_name']) > 0) { echo "The field is NOT empty!"; } ?> That doesn't mean they didn't enter bad characters You need a regular expression to do this effectively. True, that just checks if they entered anything. The question seemed pretty unclear to me. Now I see that he is probably looking for a way to restrict certain characters.[/code] Link to comment https://forums.phpfreaks.com/topic/123146-disallowing-characters/#findComment-636113 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.