Click here to Skip to main content
15,905,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want my output should come in the list format but it comes in single line.

Output:
one,two

Expected Ouput:
one
two

What I have tried:

 <script type="text/javascript">
        
  $(document).ready(function(){
  var arr = [];
 
  $("#txtResult").on("click", function(){
       text="";
        if (jQuery("#txtFirstNo").val().match(/^\d{5}$/, '')  )  {
                arr.push("one") ;
            }
        if ( jQuery("#txtFirstNo").val().match(/^\d{5}$/, '') )
            {
                arr.push("two")+"<br>";
            }
        if ( jQuery("#txtFirstNo").val().match(/^\d{7}$/, '') )
            {
                arr.push("three")+"<br>";
            }
        
        jQuery("p").text(arr) ;
        arr=[];
                
  });
});
    </script>
    
</head>
<body>


    <input type="text" id="txtFirstNo" name="txtFirstNo" placeholder="Enter value"  />
    <br><br>
    <input type="submit" id="txtResult"/><br
    <p id="txtPrint" ></p>
 
</body>
</html>
Posted
Updated 29-Oct-20 0:10am
v4

1 solution

Change your script to:
JavaScript
$(function(){
    $("#txtResult").click(function(){
        var firstNo = $("#txtFirstNo").val(); // Cache this value, since you're using it multiple times
        var arr = [];
        
        if (firstNo.match(/^\d{5}$/)) {
            arr.push("one");
            arr.push("two"); // No need for a separate "if" branch with the same condition
        }
        else if (firstNo.match(/^\d{7}$/)) { // Use "else if", since the value can't match both patterns
            arr.push("three");
        }
        
        $("#txtPrint").text(arr.join("\n"));
    });
});
Set the style on your output element so that line-breaks are preserved.
HTML
<p id="txtPrint" style="white-space:pre-line;"></p>
Demo[^]
 
Share this answer
 
Comments
Member 14978380 29-Oct-20 6:14am    
No, I have some more regex patterns that are to be used in if conditions, so I want that if both the condition gets true then the value should come in list format.
Member 14978380 29-Oct-20 6:16am    
Can I get the output in list format without changing if format ?
Richard Deeming 29-Oct-20 6:20am    
Getting the output to display on multiple lines involves two things:
* Using arr.join("\n") to join the array values with the newline character;
* Setting style="white-space:pre-line;" on the output element so that the newlines are preserved.
Member 14978380 29-Oct-20 6:22am    
But it doesn't come in the list format. So is there any other option ?
And I have used arr.join("\n") , but it doesn't affects the output.
Richard Deeming 29-Oct-20 6:30am    
Look at my demo again. If that's not the output format you want, then you need to explain what you mean by "list format".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900