Solved

Help with testing format of data entered before saving data in an XML file.

Posted on 2011-09-29
4
200 Views
Last Modified: 2012-05-12
Hello,

What is the VB.NET syntax to check the format of a text before it saves the data to an XML file?

Dim MyReceiver As XDocument = XDocument.Load(Server.MapPath("~/App_Data/Receiver.xml"))
Dim LargestReceiverID = (From el In MyReceiver.Descendants("ReceiverTable") Select CInt(el.<Receiver_ID>.Value)).ToList
        LargestReceiverID.Sort()
        Dim CheckForItemB = (From el In MyReceiver.Descendants("ReceiverTable").Elements("Receiver") Select el.Value).ToList
         If Not CheckForItemB.Contains(Me.TextBox1.Text.ToString) Then
          *****If format of Textbox1.text is "9999-99-999-9999" (syntax?) then*******
           Dim xNew As XElement = New XElement("ReceiverTable")
            xNew.Add(New XElement("Receiver_ID", (LargestReceiverID.Last() + 1)))
            xNew.Add(New XElement("Receiver", Me.TextBox1.Text.ToString))
            MyReceiver.Root.Add(xNew)
            MyReceiver.Save(Server.MapPath("~/App_Data/Receiver.xml"))
        End If
       End if


Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:CmdoProg2
ID: 36891189
Use an regular expression to check.  BTW, you can also enforce the format on the page by a RegularExpressionValidator.
' from System.Text.RegularExpressions
    Dim rx As New Regex("\d{4}-\d{2}-\d{3}-\d{4}")
    If rx.IsMatch(TextBox1.Text) Then
      'place code here to do when in correct format
    End If

Open in new window

0
 

Author Comment

by:vcharles
ID: 36891216
Hello,

Is there to execute the code while the user it typing the data in the Textbox and if there is no match to prompt him/her of the required format? How do you use the RegularExpressionValidator, I have to do this for many controls, textboxes, comboboxes, perhaps it would be best to use the RegularExpression Validator.

Thanks,

Victor
0
 
LVL 12

Accepted Solution

by:
CmdoProg2 earned 500 total points
ID: 36891338
The link is an overview of the validation controls.  Basically, the standard validation controls within a validation group are triggered on the client side by control with the causevalidation property set to true (usually a button).  A validation group is a similiar concept to a radio button group.  I use them extensively for user input for dates, numbers, and formatted data such as SSAN, telephone numbers, etc.  

http://msdn.microsoft.com/en-us/library/aa479013.aspx

To restrict what the user enters as they type, you may want to check out the MaskedEdit ASP.NET AJAX extender.  
http://www.asp.net/ajaxlibrary/act_MaskedEdit.ashx

Both controls have the concept to restrict the data format on the client side before it is sent to the server.  Of course, there is always an exception.  The customvalidator can be set up to be sent to the server, then the Page.IsValid property must be checked.  Also, JQuery can be used, but I do not have any specific knowledge on it.

Depending on your need, the validation controls handles most data entries.  I would use these before using the other options.

0
 

Author Closing Comment

by:vcharles
ID: 36891379
Thank You!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now