Wednesday, 10 August 2022

WRITE A JAVASCRIPT TO CHECK ENTERED STRING BEGIN AND END ITH OR NOT

Check whether the entered string begins and ends with X or not (X is any character of your choice) 


<!DOCTYPE html>

<html>

<body>


<p>Click the button to check where if the string starts with the specified value.</p>


Enter Name: <input type="text" name="txt_name" id="name"/> <br/>

//<input type="text" name="txt_name1" id="name1" size="1" maxlength="1"/>


<button onclick="myFunction()">Try it</button>


<p id="demo"></p>


<p><strong>Note:</strong> The startsWith() method is not supported in IE 11 (and earlier versions).</p>


<script>

function myFunction() {

  var str = document.getElementById("name").value;

  //var test = document.getElementById("name1").value;

  var n = str.startsWith

  //document.getElementById("demo").innerHTML = n + n1;

  

  if(n == true) {

document.getElementById("demo").innerHTML = "Valid String";

  }

  else {

document.getElementById("demo").innerHTML = "Invalid String";

  }

}

</script>


</body>

</html>

No comments:

Post a Comment