locked
comma Seperator in a textBox RRS feed

  • Question

  • User-1738841732 posted

    I have a textbox with jQuery validations on it
    This textBox is not allowing comma in between the values, can someone please help me out
    Below is the code

    var txtValue = $("#txtValue").val();

    txtValue = txtValue.replace(/(^\s*)|(\s*$)/gi, "").replace(/[ ]{2,}/gi, " ").replace(/\n +/, "\n");

    var Regex = /^[0-9a-zA-Z-]+$/;

    if (!Regex.test(txtValue)) {....

    Friday, January 25, 2019 6:47 PM

Answers

  • User-943250815 posted

    Try this way

    function AlphaNumericOnlyDash() {
          if (evt.which > 0 &&                             // key pressed
                evt.which == 8 ||                          // accept backspace
                evt.which == 44 ||                         // comma
                evt.which == 45 ||                         // dash
                (evt.which >= 48 && evt.which <= 57) ||    // 0-9
                (evt.which >= 65 && evt.which <= 90) ||    // A-Z
                (evt.which >= 97 && evt.which <=122)       // a-z
                ) {                                                                     
            CancelEvent();                                       
            }                                                                           
            else {                      
              evt.preventDefault();     
            }                                                                           
          
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 28, 2019 11:40 AM

All replies

  • User475983607 posted

    Add a comma to the RegEx...

    ^[0-9a-zA-Z-,]+$

    Friday, January 25, 2019 7:25 PM
  • User-1738841732 posted

    No Luck :(

    Friday, January 25, 2019 7:32 PM
  • User475983607 posted

    Works as expected on my end.   Can you provide test data and expected results? 

    Friday, January 25, 2019 7:48 PM
  • User36583972 posted

    Hi mansooraabid,

    This textBox is not allowing comma in between the values, can someone please help me out
    Below is the code

    You may can try the RegularExpressionValidator to validate a control.

              <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RegularExpressionValidator ID="valTextBox1" runat="server"
                    ValidationExpression="^[A-Za-z0-9\s]*$"
                    ControlToValidate="TextBox1"  ForeColor="Red"
                    ErrorMessage="Invalid!">
    
                </asp:RegularExpressionValidator>
    
    

    RegularExpressionValidator In ASP.NET
    https://www.codeproject.com/Tips/472728/RegularExpressionValidator-In-ASP-NET

    Best Regards,

    Yong Lu

    Monday, January 28, 2019 3:35 AM
  • User-943250815 posted

    Try this way

    function AlphaNumericOnlyDash() {
          if (evt.which > 0 &&                             // key pressed
                evt.which == 8 ||                          // accept backspace
                evt.which == 44 ||                         // comma
                evt.which == 45 ||                         // dash
                (evt.which >= 48 && evt.which <= 57) ||    // 0-9
                (evt.which >= 65 && evt.which <= 90) ||    // A-Z
                (evt.which >= 97 && evt.which <=122)       // a-z
                ) {                                                                     
            CancelEvent();                                       
            }                                                                           
            else {                      
              evt.preventDefault();     
            }                                                                           
          
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 28, 2019 11:40 AM
  • User-1738841732 posted

    Thanks! it worked.

    Monday, January 28, 2019 5:37 PM
  • User839733648 posted

    Hi mansooraabid,

    I suggest that you coud mark the solution as answer if it solved your problem.

    And this will be helpful for someone meets with the same problem. 

    Best Regards,

    Jenifer

    Monday, February 11, 2019 2:14 AM