locked
Find multiple occurance in string RRS feed

  • Question

  • User1510859543 posted

    We have an asp.net page that allows user to enter a decimal number and are using a FilteredTextboxExtender to only allow numbers and a decimal point as shown below.

                        <asp:TextBox ID="txtFoodPro" runat="server" Text='<%# Bind("FoodPro") %>' Width="50" onchange="CalcCalories(this);" />
                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                            ErrorMessage="Required entry" ControlToValidate="txtFoodPro" Display="Dynamic" />
                        <asp:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" ValidChars="0123456789." TargetControlID="txtFoodPro" />
    

    However, it still allows a user to enter something like 1.1.1 which fails when trying to do an update.  Is there a way in JavaScript or jQuery that I can find this before the postback?

    Thursday, December 26, 2019 10:24 PM

Answers

  • User-1038772411 posted

    Hello dlchase,

    We attached sample code for the .cshtml page.
    You have to add reference related to the AjaxControlToolkit in to the .cshtml page and add same reference into References into our project.
    You can download dll "AjaxControlToolkit" from https://www.dllme.com/dll/files/ajaxcontroltoolkit_dll.html

    .cshtml page

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> 
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
    <script type="text/javascript">
    
    	function IsOneDecimalPoint(evt) {
    		var charCode = (evt.which) ? evt.which : event.keyCode; // restrict user to type only one . point in number
    		var parts = evt.srcElement.value.split('.');
    		if(parts.length > 1 && charCode==46)
    			return false;
    		return true;
        }        
    </script>
    
    
    
    <asp:TextBox ID="txtFoodPro" runat="server" Text='<%# Bind("FoodPro") %>' Width="50" onchange="CalcCalories(this);" onkeypress="return IsOneDecimalPoint(event);" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required entry" ControlToValidate="txtFoodPro" Display="Dynamic" />
    <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" FilterType="Custom" ValidChars="01234567890." TargetControlID="txtFoodPro"></ajax:FilteredTextBoxExtender> 
    
    </asp:Content>

    If you still facing issue please share your .cshtml page code so that we can identify error.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 7:00 AM

All replies

  • User475983607 posted

    I know this is going to sound crazy but after 1400+ posts have you considered simply reviewing the validation server controls documentation to see what standard validation controls are available in ASP.NET?  You could start by clicking on the validation menu in the toolbox to see what there....  I see three controls that seems rather inviting; the regular expression, compare, and custom validator.  Maybe investigate each and see if one fits...  You might find two or even three...

    Thursday, December 26, 2019 11:17 PM
  • User-1038772411 posted

    Hello dlchase,

    We attached sample code for the .cshtml page.
    You have to add reference related to the AjaxControlToolkit in to the .cshtml page and add same reference into References into our project.
    You can download dll "AjaxControlToolkit" from https://www.dllme.com/dll/files/ajaxcontroltoolkit_dll.html

    .cshtml page

    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> 
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    
    <script type="text/javascript">
    
    	function IsOneDecimalPoint(evt) {
    		var charCode = (evt.which) ? evt.which : event.keyCode; // restrict user to type only one . point in number
    		var parts = evt.srcElement.value.split('.');
    		if(parts.length > 1 && charCode==46)
    			return false;
    		return true;
        }        
    </script>
    
    
    
    <asp:TextBox ID="txtFoodPro" runat="server" Text='<%# Bind("FoodPro") %>' Width="50" onchange="CalcCalories(this);" onkeypress="return IsOneDecimalPoint(event);" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required entry" ControlToValidate="txtFoodPro" Display="Dynamic" />
    <ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" runat="server" FilterType="Custom" ValidChars="01234567890." TargetControlID="txtFoodPro"></ajax:FilteredTextBoxExtender> 
    
    </asp:Content>

    If you still facing issue please share your .cshtml page code so that we can identify error.

    Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 27, 2019 7:00 AM
  • User1510859543 posted

    Yes, I considered this but I wanted a solution that was outside of the validation controls so I could use it on any page.  And the regular expression is pretty confusing.  Thanks.

    Friday, December 27, 2019 3:16 PM