0

I am aware with jQuery append method to extend something after text.

I was actually thinking about extending some special characters dynamically.

I want-

I have two text boxes with display:inline. One is for Basic phone Number and another is for Mobile number. I am getting all this information as summary after submitting with button.

I wanted to separate these two numbers with , separator. One way I could do this, By separating these with static method.

<input type="number" data-id="#Basic_phone#" /> , <input type="number" data-id="#Mobile_phone"/>

It looks like- 1234569119,9119119119

But it looks bad when there is no value in database for any one input box. Then I need to type numbers, But sometimes I don't have value for one of input boxes.

then I get only (space),(space) or 1234569119,(space) or (space),1234569119.

I want to get comma only when both numbers are there. Sorry I don't have any idea for doing it.

1
  • 1
    Check to see if $('tag').val() > 0 before adding the , Commented Apr 11, 2013 at 11:24

3 Answers 3

2

Try this Demo as given example

function abc(){
 var str="";
 if($("#Basic_phone").val().length>0)
   str=$("#Basic_phone").val();
 if(str.length>0 && $("#Mobile_phone").val().length>0 )
   str=str+",";

str=str+$("#Mobile_phone").val()

alert(str);
}


<input type="number" id="Basic_phone" /> , <input type="number" id="Mobile_phone"/>
<input type="button" value="hello" onclick="abc()"/>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use a conditional statements to append Comma(,), like the below one

     if(phoneNo != "" && mobileNo != "" ){
      //do the append code here
     }else{
        print the phone No
      }

Comments

0

How are you populating the two inputs?

If with server-side code, then just check the value before outputting the comma and second value:

if(Mobile_phone is not empty)
 show <input type="number" data-id="#Basic_phone#" /> , <input type="number" data-id="#Mobile_phone"/>
else
 show <input type="number" data-id="#Basic_phone#" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.