473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Check if a string only contains "A-Za-z0-9_" and "."

Hi all!
I'm new here!

I was wondering how I can check if a string contains only "A-Za-z0-9_" and ".".
I tried the following code

[php]
function check_validstring($check_validstring)
{
if(eregi('[A-Za-z0-9_.]',$check_validstring))
{
return true;
}
else
{
return false;
}
[/php]

The result is:

When $check_validstring = 'aaa' it passed.
When $check_validstring = '@@' it failed.
But When $check_validstring = 'aaa@@' it passed.

Any way to prevent this? Thanks!
Oct 29 '07 #1
6 3125
MarkoKlacar
296 Expert 100+
Hi,

have you looked at this?
Oct 29 '07 #2
Sure I did.
I thought they are the same thing, other than the fact that eregi is not case-sensitive.
Oct 29 '07 #3
MarkoKlacar
296 Expert 100+
Hi,

when I look at you code, it acts correctly.
What I think you can do is add an extra if-condition explicitly looking for the characters that are not allowed.

Let me know.

Cheers
Oct 30 '07 #4
Thanks for your reply.
I appriciated very very much.

I would like to know, how can I use a if to prevent this from happening?
Thanks!
Oct 30 '07 #5
MarkoKlacar
296 Expert 100+
Hi,

well the if statement could look like this:

[PHP]
else if(eregi('[@]',$check_validstring))
return false;
[/PHP]

When you do:
[PHP]
if(eregi('A-Za-z0-9_.',$check_validstring))
[/PHP]

The only thing you say is as long as there is an A or a etc the string is ok (it returns true).

Now what you can do is look at this and read about how you can limit characters, you will find something useful trust me.

Cheers
Oct 31 '07 #6
Thanks for you help!
I finally got it working.

Here's the code that should work.
[PHP]
function check_charators($string)
{
if(ereg('^[A-Za-z0-9_][A-Za-z0-9_]*$', $string))
{
return true;
}
else
{
return false;
}
}
[/PHP]

I delete the "." ;)
Oct 31 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Rick Ng Chi Wah | last post by:
Dear all, I have a text file call access.log and it contains about 5030 lines. Some lines contain some keyword, e.g. "ricky ng". How can I delete all the entries that contain "ricky ng" line and...
7
by: TastoEsc | last post by:
I'm working with a dataset table populated with people name which contains "'" and "%" symbols. How can i filter my table with Select method? These symbols aren't supported by a simple string like...
4
by: Robert W. | last post by:
I've built a complex collection object from "System.Collections.CollectionBase". With regard to it, I have a question about the "Contains" method. At first I thought that "Contains" would...
1
by: Marc Bishop | last post by:
can anyone help me with this? Code Snippet: Dim ArrCart As New ArrayList() if NOT Session("sesCart") = "" then ArrCart = Session("sesCart") Error: System.InvalidCastException: Operator is...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
35
by: pinkfloydhomer | last post by:
How do I check if a string contains (can be converted to) an int? I want to do one thing if I am parsing and integer, and another if not. /David
5
by: Curious | last post by:
I have: if (temp.Contains("Account") == true) { temp.Replace("Account", "Client"); } The "Account" is not replaced by "Client" after this operation. I used
6
by: Warly girl | last post by:
Hi i want your help again in c++ !! i want a function change the "int" to "string" because in my project "Registration system" in task "b" i must write a course class which contains number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.