简体   繁体   中英

How to Check if i have text in my input

I would like to make a small program that will check if I have any text inside the input field ... if its a number it's easy i just do

cardsp.value >= 1    

But when the input is text how can I check if there is anything in the field?

Name : <input id="namep" type="text" />

I tried to do something like that (js) but that probably the most wrong way that someone ever does

    var array= ["","a","b","c","d","e","f","g","h","i","j","k","m","n","l","o","p","q","r","s","t","u","v","w","x","y","z"];
function pay()  {
    namep.value 
    if(namep.value >= array.indexOf("a"), cardsp.value >= 1 , emailpa.value >= 1){

         alert('Ty For Buying a Ticket From our compeny , you will receive the Invoicing in the ' +emailpa.value);
        }

    else if(namep.value == array.indexOf(""){
            alert('Please Write Your Name');
    }

    }

You could check if input field contains only letters that way:

var letters = /^[a-z]+$/;  
if( namep.value.match(letters) ) {
  // string contains only letters.
}
 var input=$("#namep").val();

or

var input = document.getElementById('namep').value;

and then check if there is something...

if(input.length>0)
{
 //do something. 
}

or

var inputLength = input.length;

if(inputLength>0)
{
 //do something. 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM