You must Sign In to post a response.
  • Category: ASP.NET

    How to validate separator in textbox in asp.net


    Are you looking for a way to validate separator in textbox in asp.net ? then read this thread to know how to validate it



    I need validation to does not allow |(separator) in the string,rest all the thing are allowed except |(separator)
  • #743997
    Hai Ali,
    You can use the Key press event to not allow this separator. First find out the key value of the separator and then use the code to handle the key-press event.
    You can use the below link where you can find the validation expressions for the characters, special characters etc:
    http://hwang.cisdept.csupomona.edu/swanew/Text/Input-Validation.htm

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #744006
    Hi,

    Try the below code.

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (e.KeyChar == 124)
    {
    MessageBox.Show("Pipe Symbol not allowed");
    }
    }

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

  • #744007
    Only stop to press the Pipe Symbol (|) use the below code.

    private bool nonNumberEntered = false;

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (nonNumberEntered == true)
    {
    e.Handled = true;
    }
    }

    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
    nonNumberEntered = false;

    if (e.KeyCode == Keys.OemPipe)
    {
    nonNumberEntered = true;
    }

    if (Control.ModifierKeys == Keys.Shift)
    {
    nonNumberEntered = true;
    }
    }

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/

  • #744020
    You can use javascript for it, while button click check if textbox contains separator in string,
    use javascript 'indexOf' method to check separator in input string
    see below code snippet for more detail

    <script language="Javascript">
    fucntion checkSeparator()
    {
    var getInput = document.getElementById("txtInput").value;
    if(getInput.indexOf("|") != -1)
    {
    alert("Plese do not use separator | in input text");
    return false;
    }
    }
    </script>

    hope it help

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments