Jump to content

[SOLVED] Check for non hexadecimal chars in a string


soadlink

Recommended Posts

This is probably very easy, I just cant think of a SIMPLE way to do it:

I'm looking for a php function that will search a string and output whether or not the string contains all hexadecimal characters or not.
So here is a sample:

[code=php:0]$string = 'abcdef1234567890z';
// function to check it
echo 'Your string does NOT contain only hexadecimal characters';[/code]


or if the string was 'abcdef1234567890',

[code=php:0]echo 'Your string contains all hexadecimal characters.';[/code]



Also on a side note, I need a simple way (javascript code maybe) to disable a "Submit" button in an html form until a certain number of characters have been entered into a text box that is located in that same form. Any help there would be good too (will most like need some javascript).

Thanks  8)
Link to comment
Share on other sites

See [url=http://www.php.net/manual/en/function.preg-match.php]preg_match[/url]

Something along the lines of:

[code]
if (preg_match("/^[0-9a-fA-F]$/",$string)){
  //it matches only hexadecimal
} else {
  //it doesn't
}
[/code]

(untested)
Link to comment
Share on other sites

I did say it was untested! Sorry about that, try:
[code]
if (preg_match("/^[0-9a-fA-F]+$/",$string)){
  //it matches only hexadecimal
} else {
  //it doesn't
}
[/code]

You can change the "+" to a "*" to allow empty strings
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.