Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

821 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