Results 1 to 13 of 13

Thread: validation textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2006
    Posts
    176

    validation textbox

    Hi All.
    I validate TextBox for numeric and legth no more then 7 characters. I'm checking for Not IsNumber and Len. But I want to give a user to save record when TextBox is blank. If is it possible how to do it?
    Thanks.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: validation textbox

    Hey,

    I am not sure that I fully understand the question?!?

    Can you provide more details, plus the code you are already using?

    Gary

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: validation textbox

    Code:
    Dim BogusDec As Decimal
    If (TextBox.Text.Trim.Length = 0I OrElse TextBox.Text.Trim.Length = 7I) AndAlso Decimal.TryParse(TextBox.Text.Trim, BogusDec) Then
      'Good to go
    End If
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: validation textbox

    Hey,

    If that is what the OP is trying to do, then I would be tempted to implement this as a Regular Expression check:

    http://www.java2s.com/Tutorial/CShar...oraTextBox.htm

    Hope that helps!!

    Gary

  5. #5
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: validation textbox

    Not Sure but do you have something like this in vb.net?

    vb Code:
    1. Dim BogusDec As Decimal
    2. If TextBox.Text.Trim.Length < 8I AndAlso Decimal.TryParse(TextBox.Text.Trim, BogusDec) Then
    3.   'Good to go
    4. End If
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: validation textbox

    Regex is kind of over kill for this simple validation.
    Set the textbox.MaxLength = 7 and handle the textbox.validating event. In the event handler, just test if the text is not blank and do a tryparse to make sure it's a valid number.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: validation textbox

    Quote Originally Posted by koolsid View Post
    Not Sure but do you have something like this in vb.net?

    vb Code:
    1. Dim BogusDec As Decimal
    2. If TextBox.Text.Trim.Length < 8I AndAlso Decimal.TryParse(TextBox.Text.Trim, BogusDec) Then
    3.   'Good to go
    4. End If
    You don't need to declare that variable if you won't be using it. You can simply pass Nothing for it.
    vb.net Code:
    1. If TextBox.Text.Trim.Length < 8 AndAlso Decimal.TryParse(TextBox.Text.Trim, Nothing) Then
    2.   'Good to go
    3. End If
    [/QUOTE]
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: validation textbox

    Quote Originally Posted by Pradeep1210 View Post
    You don't need to declare that variable if you won't be using it. You can simply pass Nothing for it.
    vb.net Code:
    1. If TextBox.Text.Trim.Length < 8 AndAlso Decimal.TryParse(TextBox.Text.Trim, Nothing) Then
    2.   'Good to go
    3. End If
    Didn't know that one, I thought it had to be an actual variable whether you use it or not. It would be nice if the String class had a .IsNumber function like the Char class does.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: validation textbox

    Its easy to create extension methods.

    e.g.
    vb.net Code:
    1. Public Module MyExtensionMethods
    2.     <Extension()> _
    3.     Public Function IsNumber(ByVal input As String) As Boolean
    4.         Return Decimal.TryParse(input, Nothing)
    5.     End Function
    6. End Module

    So now you use it just like any other method of the String class:
    e.g.
    vb.net Code:
    1. Dim myString as String = "12345"
    2. MessageBox.Show(myString.IsNumber)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: validation textbox

    Didn't know that one, I thought it had to be an actual variable whether you use it or not. It would be nice if the String class had a .IsNumber function like the Char class does.
    I guess it would be nice, but you can just use the IsNumeric function instead
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: validation textbox

    Quote Originally Posted by chris128 View Post
    I guess it would be nice, but you can just use the IsNumeric function instead
    The IsNumeric function is good for ordinary use. But it usually won't give the results you would expect. It actually tries anything that can be possible as a number. e.g. hex numbers, commas etc. which you might not expect in normal ways.

    e.g.
    vb.net Code:
    1. Decimal.TryParse("&H10A", Nothing)          '<-- Returns False
    2. IsNumeric("&H10A")                          '<-- Returns True
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: validation textbox

    Oh right, I never knew that, thanks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width