none
Textbox start with number and not , or . RRS feed

  • Question

  • I have a textbox that i write to a database , this must have a number and max 2 decimals.

    Or only number and no decimals

    Charactersallowed are 

      Dim charactersAlloweds As String = "1234567890.,"

    When it is empty or only a , or . i use this

    If String.IsNullOrWhiteSpace(Kmstartreis.Text) Or Kmstartreis.Text = (",") Or Kmstartreis.Text = (".") Then
                MsgBox("No good.")
                Kmstartreis.Text = ("0")
            End If

    But what do i use to prevent that there is first a , or . then decimal ?

    First a number then , or .
    for example
    0.22 that is oke
    
    This not 
    ,22
    or 
    .22

    It is only a example it could be 6 numbers then a , or .

    So first a number then , or . 



    • Edited by KeesBlunder Sunday, October 7, 2018 1:57 PM
    Sunday, October 7, 2018 1:55 PM

Answers

  • thanks Les,

    That is working , when start with period it is changing to 0.

    BTW, can an input contain both a comma and a period?

    This is something that is comming to my mind now you say so , the outcome must only be 1 comma.

    Sometimes the user is using . and not , when entering some numbers


    Hi

    Tty this version. I have tried to make sure end result is numeric with only one "," allowed (lets user put a "." but changes it to a ",")

      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        t.Text = t.Text.Replace("."c, ","c)
    
        If t.Text.StartsWith(".") Or t.Text.StartsWith(",") Then t.Text = "0" & t.Text
        t.SelectionStart = t.MaxLength
        t.SelectionLength = 0
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789,.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "," And tb.Count(Function(c) c = "," AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Or (e.KeyChar = "." And t.Text.Contains(",")) Then
          e.Handled = True
        End If
      End Sub


    Regards Les, Livingston, Scotland



    • Edited by leshay Sunday, October 7, 2018 8:09 PM
    • Marked as answer by KeesBlunder Sunday, October 7, 2018 8:33 PM
    Sunday, October 7, 2018 8:07 PM

All replies

  • Hi

    Here is some code that limits a TextBox entry. This particular one

        ' allows only "0123456789.+-"
        ' And BackSpace

        ' allows "-+" only as first char.
        ' allows "." only once and
        ' NOT as first character.

    and no doubt could be rearranged to suit any requirement.

    ' Form1 with TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
    
      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        ' allows only "0123456789.+-"
        ' And BackSpace
    
        ' allows "-+" only as first char.
        ' allows "." only once and
        ' NOT as first character.
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
          e.Handled = True
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, October 7, 2018 5:04 PM
    Sunday, October 7, 2018 2:13 PM
  • "But what do i use to prevent that there is first a , or . then decimal ?"


    If I understand the question you need to format the number before saving ie always add a zero before the decimal point.

    Imports System.Globalization
    
    Public Class Form6
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            TextBox1.Text = ".56"
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim myString As String = CDbl(TextBox1.Text).ToString("0.000", CultureInfo.InvariantCulture)
    
            MsgBox(myString)
    
        End Sub
    End Class

    Sunday, October 7, 2018 2:37 PM
  • Thanks tommytwotrain ,

    That is working .

    Is there a way to use the , and not  .

    So i can save it in my database

    Sunday, October 7, 2018 5:31 PM
  • Thanks tommytwotrain ,

    That is working .

    Is there a way to use the , and not  .

    So i can save it in my database


    Oh, well I was hoping the culture part did that according to your windows culture settings but I guess not?

    What is your systems windows lang/numeric setting comma or period?

    What happens if you use a comma instead of period between the quotes?

    There should be a way to do it for the current culture setting but I am not sure now that you bring it up.

    Les probably knows arent you a comma country?

    Sunday, October 7, 2018 6:04 PM
  • Country is Nederland ( Dutch)

    Windows settings says , 

    When i use this  with comma

     Dim myString As String = CDbl(Kmstartreis.Text).ToString("0,00", CultureInfo.InvariantCulture)
    .99 
    becomes
    001
    
    and
    4,99 
    becomes
    005
    

    I was have trouble with some database calculations and saving  when i used a dot


    Sunday, October 7, 2018 6:27 PM
  • Thanks for your reply Les ,

    i used the code but i still can start with a comma or dot.

    Only 1 dot is a nice thing to ad that i forgot

     Dim t As TextBox = DirectCast(sender, TextBox)
    
            Dim tb() As Char = t.Text.ToCharArray
    
            Dim allowed As String = "0123456789.," & Chr(8)
    
            If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
                e.Handled = True
            End If

    Sunday, October 7, 2018 6:37 PM
  • Kee,

    Yes well I am going to leave that for Les or Karen et al I think they know the proper way to do that. Or maybe you can find it in in the link I gave you and etc.

    :)

    Sunday, October 7, 2018 6:39 PM
  • tommytwotrain,

    In the link you gave me i found something that is working. N2 for 2 behind the comma

     Dim myString As String = CDbl(Kmstartreis.Text).ToString("N2", CultureInfo.CreateSpecificCulture("sv-SE"))

    Don't know what this means 

    ("sv-SE")
    the Link

    Sunday, October 7, 2018 6:59 PM
  • Thanks for your reply Les ,

    i used the code but i still can start with a comma or dot.

    Only 1 dot is a nice thing to ad that i forgot

    Hi

    Try this one. BTW, can an input contain both a comma and a period?

      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        ' allows only "0123456789.+-"
        ' And BackSpace
    
        ' allows "-+" only as first char.
        ' allows "." only once and
        ' NOT as first character.
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        If t.Text.StartsWith(".") Or t.Text.StartsWith(",") Then
          t.Text = "0" & t.Text
          t.SelectionStart = t.MaxLength
          t.SelectionLength = 0
        End If
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789,.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "," And tb.Count(Function(c) c = "," AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
          e.Handled = True
        End If
      End Sub


    Regards Les, Livingston, Scotland

    Sunday, October 7, 2018 7:13 PM
  • thanks Les,

    That is working , when start with period it is changing to 0.

    BTW, can an input contain both a comma and a period?

    This is something that is comming to my mind now you say so , the outcome must only be 1 comma.

    Sometimes the user is using . and not , when entering some numbers



    Sunday, October 7, 2018 7:37 PM
  • thanks Les,

    That is working , when start with period it is changing to 0.

    BTW, can an input contain both a comma and a period?

    This is something that is comming to my mind now you say so , the outcome must only be 1 comma.

    Sometimes the user is using . and not , when entering some numbers


    Hi

    Tty this version. I have tried to make sure end result is numeric with only one "," allowed (lets user put a "." but changes it to a ",")

      Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        t.Text = t.Text.Replace("."c, ","c)
    
        If t.Text.StartsWith(".") Or t.Text.StartsWith(",") Then t.Text = "0" & t.Text
        t.SelectionStart = t.MaxLength
        t.SelectionLength = 0
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789,.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = "." AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "," And tb.Count(Function(c) c = "," AndAlso tb.Count > 0) > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Or (e.KeyChar = "." And t.Text.Contains(",")) Then
          e.Handled = True
        End If
      End Sub


    Regards Les, Livingston, Scotland



    • Edited by leshay Sunday, October 7, 2018 8:09 PM
    • Marked as answer by KeesBlunder Sunday, October 7, 2018 8:33 PM
    Sunday, October 7, 2018 8:07 PM
  • Your right, i am way of

    Not going to use it.

    When i use a large number it is going wrong.

    12455.36
    
    the outcome is
    
    12 455,36

    Big trouble with the space.

    I think i am using Les his last solution.

    Thanks for the reply's 

    Sunday, October 7, 2018 8:31 PM
  • Thanks Les,

    Saving this one , only 1 comma is a good tip.


    Sunday, October 7, 2018 8:34 PM