Jump to content

Disallowing Characters.


al3x8730

Recommended Posts

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>

 

 

[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]

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
}

 

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?

[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]

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.