9阅网

您现在的位置是:首页 > 知识 > 正文

知识

javascript - 如何使用javascript检查一个变量是否为特定类型?

admin2022-11-07知识15

我是一个网站开发的初学者,我有一个HTML表格,一个人可以添加他的地址,地址号码,地区和邮政编码。在这个表单中,地址和地区必须只包含char字母。

(ex. Lakewood : correct Lakewood13 : error)。如果这两个变量中的任何一个包含一个数字,我必须再次输入我的数据继续。否则,我移动到下一页。我是一个完全的初学者,在javascript,我需要使用检查我的变量类型,我会感谢你的帮助,指导我解决这个问题。这是我的代码与我的HTML表格的地址号码和区域,这是我们需要在这个问题上的变量。

function checkdata(){

  //the two elements we need to check
  var a = document.getElementById("address");
  var r = document.getElementById("region");

  if(typeof(a.value) === 'string'&&(typeof b.value) ==='string'){
  //continue to next page(but how can I check if numbers are in the strings ?)
  }

  else{
    //go back to form and enter again(how can I enter the elements again ? )    

  }

}
<div class = "form-area" id = "forma"> 

  <form action="/action.page.html" class = "sign-form" > 

    <div class = "form-container">

      <h1> Enter purchase data below : </h1> 

      <label for="addrs">  Address Name</label>
      <input type = "text" placeholder = "Enter address name "  id = "address" name = "addr" required/> 

      <label for="regn" > Region </label>

      <input type = "text" placeholder = "Enter region " id = "region" name = "reg" required/>  
    </div>
    <button type="submit" onclick = "checkdata()">Continue</button> 
  </form>

</div>

先谢谢你。



【回答】:

你可以尝试使用regex来检查字符串中是否包含任何数字。

if(!(/\d/.test(a.value)) && !(/\d/.test(b.value))){

请注意: 你还必须 return false 以防止默认的 事件 如果条件 假的 和前缀 返回 中的函数调用 点击 属性。

演示一下。

function checkdata(){
  //the two elements we need to check
  var a = document.getElementById("address");
  var r = document.getElementById("region");
  if(!(/\d/.test(a.value)) && !(/\d/.test(r.value))){
    alert('form submit');
  }
  else{
    alert('no submit');  
    return false;
  }
      
}
<div class = "form-area" id = "forma"> 
  
  <form action="/action.page.html" class = "sign-form" > 

    <div class = "form-container">

      <h1> Enter purchase data below : </h1>   

      <label for="addrs"   Address Name</label>
      <input type = "text" placeholder = "Enter address name "  id = "address" name = "addr" required/> 

      <label for="regn" > Region </label>

      <input type = "text" placeholder = "Enter region " id = "region" name = "reg" required/> 

    </div>
    <button type="submit" class="continuebtn" onclick = "return checkdata()">Continue</button>

  </form>

 </div>
【回答】:

你可以写一个有效性函数,然后你可以根据这个函数来检查依赖性**。

function checkData() {
	let adress = document.getElementById('address');
	let region = document.getElementById('region');

	function isValid(e) {
		let isTrue;
		for (let char in e) {
			typeof e[char] !== 'string' ? alert('Please only type strings') : (isTrue = true);
		}
		return isTrue;
	}
	isValid(adress.value) && isValid(region.value) ? console.log('next page') : console.log('error');
}

checkData();
【回答】:

所以需要检查字符串中是否含有数字。

希望你能在这里找到更多的启示。用javascript检查输入字符串是否包含数字

工作演示。

// check if string contains number
function hasNumber(myString) {
    return /\d/.test(myString);
}


function checkdata(e) {
    e.preventDefault()
    //the two elements we need to check
    var a = document.getElementById("address");
    var r = document.getElementById("region");
    var isAddressContainsNumber = hasNumber(a.value);
    var isRegionContainsNumber = hasNumber(r.value);

    console.log(isAddressContainsNumber, isRegionContainsNumber)

    if (isAddressContainsNumber === false && isRegionContainsNumber === false) {
        console.log('None of string contains number')
    } else {
        console.log('One or Both  string contains number')
    }


}


const form = document.querySelector('.sign-form');

form.addEventListener('submit', checkdata);
<div class="form-area" id="forma">
    <form class="sign-form">
        <div class="form-container">
            <h1> Enter purchase data below : </h1>
            <label for "addrs" Address Name</label>
            <input type="text" placeholder="Enter address name " id="address" name="addr" required/>
            </label>
            <label for "regn" > Region </label>
            <input type="text" placeholder="Enter region " id="region" name="reg" required/>
            </label>
        </div>
        <button type="submit" class="continuebtn">Continue</button>
    </form>
</div>
【回答】:

我建议通过字符串,获取每个字符的ASCII值。数字0-9是ASCII字符48-57。Javascript使用UTF-16,相应的方法(charCodeAt)会返回一个16位的UTF-16值,但UTF-16字符0-127符合ASCII。所以。

var testString = "abcd123";
var isValid = true;

for (var i=0;i<testString.length;i++)
{
 if (testString.charCodeAt(i) > 47 && testString.charCodeAt(i) < 58)
 {
  isValid = false;
 }
}

if (!isValid)
{
 //Code here to alert the user
 alert("There's a number in there!");
}
【回答】:

你使用typeof的方式不对,试试这样的方式typeOf(你想检查的变量)