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>