Solved

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

Posted on 2011-09-29
4
201 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

16 Experts available now in Live!

Get 1:1 Help Now