locked
validation for numbers with comma only RRS feed

  • Question

  • User-240513752 posted

    I have  to validate a number enter by user with separted by comma only I am making insertion sort in javascript not jquery etc..

    like 1,8,9,5,2,7

    5,5,7,9,9

    -8,5,-7,6,8

    the limit of number will be 5.

    Sunday, February 24, 2019 2:03 PM

All replies

  • User475983607 posted

    I have  to validate a number enter by user with separted by comma only I am making insertion sort in javascript not jquery etc..

    like 1,8,9,5,2,7

    5,5,7,9,9

    -8,5,-7,6,8

    the limit of number will be 5.

    IMHO. this is a fairly basics programming problem that someone with your experience should be a able to solve.  Please share your JavaScript code.  Explain the expected results and the actual results.

    Sunday, February 24, 2019 2:53 PM
  • User-240513752 posted

    I know It is very basic, I have done it in c# without validation, now i want to do insertion sort in javascript with html, I

    I need to validate only by javascript and have to show by alert that there would be only number with separation of comma.

    <html>
    <head>
    
      <script>
       function Render(){
    	var myName=frmMain.txtname.value;	
    	alert(myName); // here I need validation if number is not given by user with separated by comma, that could be an array separated by comma.
    }
    
     
    
    
      </script>
    
    </head>
    <body>
      <form name="frmMain">
    	<label> Enter any five numbers by separating comma </label>
            <input type="text" name="txtname" />
             
    <input type="submit" value ="Submit" onclick="Render()"/>
    </form>
    </body>
    
    
    </html>

    Sunday, February 24, 2019 6:10 PM
  • User475983607 posted

    No valuation logic posted!  Share your code and explain the expected results and actual results.

    Sunday, February 24, 2019 6:56 PM
  • User-893317190 posted

    Hi Khan_1,

    Not clear about your requirement, do you mean you want to validate numbers separated by comma and its max number count is five?

    If so , you could try regex of javascript.

    var regex = /^(-?\d+\,){0,4}(-?\d+)$/;   //number after which there is a comma appears 0-4 times, number without comma appears for one time, - is allowed
    regex.test("11,-12,9")

    You could also use string.split to split the string with comma to see whether the returned array's length smaller then 5, and validate every element of the array to see whether it is a number.

    Best regards,

    Ackerly Xu

    Monday, February 25, 2019 4:29 AM