Solved

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

Posted on 2011-09-29
4
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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