0

I'm a beginner in web development and I have an HTML form where a person can add his address , address number, region and postal code . In this form the address and the region have to contain only char letters .

(ex. Lakewood : correct Lakewood13 : error) . If any of these two variables contains a number I have to enter my data again to continue . Else, I move to the next page . I'm a complete beginner in javascript which I need to use to check my variable types and I would appreciate your help with guiding me to solve this problem . This is my code with my HTML form with the address number and the region which are the variables we need in this problem :

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" class="continuebtn" onclick = "checkdata()">Continue</button> 
  </form>

</div>

Thank you in advance .

Mamun
  • 66,969
  • 9
  • 47
  • 59
Vasilis Skentos
  • 506
  • 11
  • 22

5 Answers5

1

You can try using regex to check if string contains any number in it:

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

Please Note: You also have to return false to prevent the default event if the condition is false and prefix return the function call in onclick attribute.

Demo:

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>
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

You can write a function for validity, then you can check for dependencies based on that **

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();

**

0

So need to check if the strings are containing numbers or not

hope you find more insight here: Check whether an input string contains a number in javascript

working demo :

// 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>
Sandesh Sapkota
  • 747
  • 1
  • 6
  • 18
0

I would recommend going through the string and getting the ASCII value of each character. Numbers 0-9 are ASCII characters 48-57. Javascript uses UTF-16 and the appropriate method (charCodeAt) returns a 16-bit UTF-16 value, but UTF-16 characters 0-127 match ASCII. So:

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!");
}
jmccracken
  • 53
  • 8
-1

You are using typeof in wrong way, try this way typeOf(variable you want to check)

Zeeshan Awan
  • 163
  • 1
  • 5
  • 14