473,804 Members | 3,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Validating data

I have data that is in this format: 8-8-19-29-1-4-41
I wanted to write a simple function or regular expression that
verified that data is in that format. The numbers should only be 1 or
2 digits. There should be 6 dashes (-) with 7 numbers around the
dashes. Here are examples of bad data:
8-19-29-1-4-41 (Only 6 dashes)
5-1-2-3-4-S-67-4 (Includes a letter)
5-1-2-3-4-222-67-4 (has three digits in one space)

Apr 27 '07 #1
5 1373
On Apr 27, 11:22 pm, Anthony Smith <mrsmi...@hotma il.comwrote:
I have data that is in this format: 8-8-19-29-1-4-41
I wanted to write a simple function or regular expression that
verified that data is in that format. The numbers should only be 1 or
2 digits. There should be 6 dashes (-) with 7 numbers around the
dashes. Here are examples of bad data:

8-19-29-1-4-41 (Only 6 dashes)
5-1-2-3-4-S-67-4 (Includes a letter)
5-1-2-3-4-222-67-4 (has three digits in one space)

heres one using ereg
$var = '1-2-3-44-55-66-77';
if ( ereg( "^[0-9]{1,2}(-[0-9]{1,2}){6}$", $var ) )
{
//ok cos it starts with 1 or 2 characters from range 0-9,
//then has exactly 6 groups of - followed by 1 or 2 digits
}

Apr 27 '07 #2
Rik
Anthony Smith wrote:
I have data that is in this format: 8-8-19-29-1-4-41
I wanted to write a simple function or regular expression that
verified that data is in that format. The numbers should only be 1 or
2 digits. There should be 6 dashes (-) with 7 numbers around the
dashes. Here are examples of bad data:
8-19-29-1-4-41 (Only 6 dashes)
5-1-2-3-4-S-67-4 (Includes a letter)
5-1-2-3-4-222-67-4 (has three digits in one space)
Regex is one way to go, depending on the source, fscanf() can be your
friend too.

--
Rik Wasmus

Estimated date being able to walk again: 01-05-2007.
Less then a week, hurray!
Apr 28 '07 #3
On Sat, 28 Apr 2007 17:52:33 +0200, Rik wrote:
Anthony Smith wrote:
>I have data that is in this format: 8-8-19-29-1-4-41
I wanted to write a simple function or regular expression that
verified that data is in that format. The numbers should only be 1 or
2 digits. There should be 6 dashes (-) with 7 numbers around the
dashes. Here are examples of bad data:
8-19-29-1-4-41 (Only 6 dashes)
5-1-2-3-4-S-67-4 (Includes a letter)
5-1-2-3-4-222-67-4 (has three digits in one space)

Regex is one way to go, depending on the source, fscanf() can be your
friend too.
The latter I rarely see in PHP code, but indeed Rik, format strings are a
great partner sometimes. And of course <winks at Steveespecially useful
for people who don't "trust" regex ;-) In this case I think fscanf() is
highly recommendable considering the fixed pattern.

Regex is a great tool in the right hands, in many cases a great trick for
obfuscating what you're doing to donwstream programmers using your code,
and often abused where far easier and less resource intensive string
functions could/should be used. Regex IS relatively costly in cpu cycles,
no doubt about it, but it can, when used correctly, sometimes replace a
pageful of ugly string manipulation code with one single call.

Nonetheless a lot of people avoid learning regex like the plague, because
it looks so complicated. That's a pity. There are plenty books & online
sources for learning regex and a plethora of little regex 'trainer' apps,
some freeware, some at a reasonable fee.

The only thing I still haven't been able to conjure up is a nifty regex
expression that takes the typical erratic female thoughtprocess as input
and churns out the rationale behind it ;-)

Sh.
Apr 29 '07 #4

"Schraalhan s Keukenmeester" <in*****@invali d.spamwrote in message
news:pa******** *************** *****@invalid.s pam...
and churns out the rationale behind it ;-)
That may be more to do with the assumption of "rationale" rather than an
actual inadequacey in the expresion engine @@.
>
Sh.

Apr 29 '07 #5
Schraalhans Keukenmeester wrote:
Regex IS relatively costly in cpu cycles, no doubt about it, but it can,
when used correctly, sometimes replace a pageful of ugly string
manipulation code with one single call.
Regular expressions are one of those examples of sacrificing CPU time in
order to make the code easier to write, easier to read and easier to
maintain.

This is nearly always a beneficial trade.

Other examples:

- Comments in source code;
- Use of constants;
- SQL databases; and
- Logically dividing library functions into multiple files.

--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4248
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end of the post. I am using VS.net 2003, .Net 1.1, Win2k Server I have a simple schema and a simple XML document.
1
1377
by: Mike | last post by:
Hi, In one of my winforms I am validating a textbox making sure it was filled out and that the data is numeric and setting e.Cancel = true if not. Everything works fine however the form has a OK and Cancel button. I do not want the user be able to submit the form if the data is not correct (which works fine by setting e.Cancel = true) however I want to the user to be able to hit cancel and which closes the form. Right now by setting...
0
1609
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works correctly the first time, but if I input some data in the form and click another control to change focus, the validator fires, but if I continue to hit 'ESC' enough times, it eventually lets me out of the validating loop and moves focus to the other...
4
3040
by: Eric | last post by:
Is there a way to cancel the validating event on the closing event? I have 2 textboxes that I use the validating event to check for numeric data. If I try to close the form without putting a number in, it calls the validate event and pops up my MsgBox. Thanks!
0
2441
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852 (http://support.microsoft.com/kb/810852), but then I realized that the hotfix mentioned was in .Net v1.1, which I am using. I took the sample from that article and recreated the situation I see in my application. (Code included below.) If you run the...
6
1927
by: J Huntley Palmer | last post by:
What is the most efficient way to validate an input to conform to your needs? I need to make sure an input is a contiguous string with only printable characters (english alphabet+numbers only) and no whitespace or punctuation marks. Thanks
4
2484
by: easoftware | last post by:
I am using VS .Net 2003 and VB. I have an app with one parent and two Mdi child forms. I need to validate data in the Mdi form. The Form.Validating event works when I try to close a Mdi form, but not when I try to switch form one Mdi form to the other. I tried to add code to MdiForm1's Deactivate event: Private Sub MidForm1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Deactivate Dim TempE As...
5
4820
by: ameen.abdullah | last post by:
Hi Guys, I have a textbox in windows form that should only accept alphabets, numbers, spaces and underscore. If the textbox contains anyother character it should display a msg at the time of validation.. Is there any funciton in vb.net for this? or any other way?? Waiting for ur replies...
1
1308
by: lenin42001 | last post by:
Help i've just done a basic calculator with three textboxes a label & a button, basically when you add a number in textbox1 to the one in textbox2 it appears in textbox3 .........now the question asks to check if the user has typed in something appropriate . It says double click on txtnum1 use right hand list box to find Validating( ) event, then modify event stub so it looks like this If Not IsNumeric(txtNum1.text) Then e.Cancel =True End if ...
8
4133
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has prevailed it just is :) My problem is this im trying to validate the contents of a textbox (it has to be a normal textbox) and on a c# winforms and i am calling the textbox validating
0
9704
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.