Check for a specific character string using javascript
How can I write ad check for a specific character string in a text field?
If someone types in this identification number in a textfield.
Upon submit, it will look if it starts with "D" IE: D959344843If it does, display a DIV on the page. If not, prompt an error alert message.
-=-=-=-=-=-=-=-=-=-=-=--=
New addition How can I code this on my page and make it work?<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Test</title>
<script type="text/javascript">
var mystring = 'D59344843';
if(mystring.substring(0, 1) == 'D'){
//display a DIV
alert('cool');
}else{
alert('error is no kewl');
}
</script>
</head>
<body>
<input name="yourtextfield" value="" type="text" />
<input name="" type="submit" />
</body>
</html>
string = 'D959344843';
x = string.match(/^D/i);
if(x){
//function to show div
}
This is using regex. If the character ([d]) is at the beginning of the string (^), this will match and return true. The /i makes the query non-case sensitive.
Another way to do this would be
string = 'D959344843';
if(string.charAt(0) == 'D' || 'd'){
//function to show div
}
This will look for the character at position 0 in the string (the first character). If it is a D or a d, it will do whatever is in the if block.
var mystring = 'D59344843'; //or for your textfield w/ id document.getElementById('yourtextfield').value;
if(mystring.substring(0, 1) == 'D'){
//display a DIV
alert('cool');
}else{
alert('error is no kewl');
}
Update:
Here is the jsfiddle
html:
<input name="yourtextfield" id="yourtextfield" value="" type="text" />
<a href="#" onClick="checkMahStr()">Test</a>
<div id="class1" style="display: none">afasdfdsafsdafdsafsdfsdaf</div>
JavaScript:
function checkMahStr() {
var mystring = document.getElementById('yourtextfield').value;
if (mystring.substring(0, 1) == 'D') {
document.getElementById('class1').style.display = 'block';
} else {
document.getElementById('class1').style.display = 'none';
alert('error is no kewl');
}
}
Checking if a string contains a substring can be achieved by using regular expressions. Here's an example:
var r = /^D/;
r.test('Hello World !'); // Returns false
r.test('D9756612'); // Returns true;
More information about regular expressions can be found here
If that doesn't help you, please describe your problem more precisely !
EDIT: Mis-read your example and corrected accordingly. If you just want to check the first character of a string, go for ferrari fan's answer, using indexOf()
. If you want to be able to do multiple checks at the same time, I recommend you use regular expressions rather than multiple conditional structures.
You could just use the indexOf() function
if (myString.indexOf('D') == 0) {
... perform your logic here since 'D' was found ....
} else {
... your alert goes here ...
}
精彩评论