Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
function validPhone(phoneNum) {
var reg = new RegExp("^\d{3}-\d{4}$");
if (reg.test(phoneNum)) {
return "True";
}
else {
return "False";
}
}
//main code\\
<html>
<head>
<script language="javascript" type="text/javascript" src="codechallenge3.js"></script>
</head>
<body>
<script>
var phoneNum = "555-555";
document.write("Check the following phone number: ", phoneNum, " = ", validPhone(phoneNum), "<br>");
</script>
</body>
</html>
//Why does it keep returning false? I've tried to fix it , but it's not returning true.\
Your regular expression is looking for 4 digits not 3. Try 555-5555
Valid phone number would be ddd-dddd where d is a digit.
You have 2 problems:
Change
var phoneNum = "555-555";
to
var phoneNum = "555-5555";
if you want to have 4 digits in the second group.
Change
var reg = new RegExp("^\d{3}-\d{4}$");
to
var reg = new RegExp("^\\d{3}-\\d{4}$");
or to
var reg = new RegExp("^[0-9]{3}-[0-9]{4}$");
or to
var reg = /^\d{3}-\d{4}$/;
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I think my concatenating is fine, but my use of it isn't working. Any help would be appreciated. Thanks.
<script>
var str1 = "/images/";
var str2 = "test.jpg";
var res = str1.concat(str2);
</script>
<div class="product-sprite" data-image="res"></div>
DOM element attributes are not evaluated as variables. You need to assign it to the element property.
var res = str1 + str2; // no need to use .concat()
document.querySelector(".product-sprite").dataset.image = res;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have the following
var string = '1,7,12,15,16,29';
if I just want to replace the number 1, i will use the following
removeStr(1);
function removeStr(str1)
{
var string = '1,7,12,15,16,29';
var newstr = string.replace(str1, '');
alert('new val is ' + newstr);
}
But doing this, will end up removing the number 1 in 12,15,16.
How do I just remove the exact match 1 in this example.
Thanks
You could use boundaries (\b) in a regexp that to match a whole word only. Changed your test string to one where your question would be applicable
function removeStr(str1)
{
var string = '11,71,12,1,16,21';
var newstr = string.replace(new RegExp("\\b"+str1+"\\b"), "");
console.log('new val is ' + newstr);
}
removeStr("1");
function replaceOne(str1, str2){
var arr = str2.split(",");
var newStr = "";
for(var i=0; i<arr.length; i++){
if(arr[i]!=str1){
newStr = (newStr=="")?arr[i]:newStr+","+arr[i];
}
}
console.log(newStr);
}
You are trying to do it on strings.
you might consider to make it an array
var string = '1,7,12,15,16,29';
var arr=string.split(",");
var newArr=arr.splice("1");
string=newArr.join(",");
console.log(string);
Hope this helps
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm studying for an exam and I'm doing some sample problems by myself. I've ran into the Uncaught TypeError: document.getElementByID is not a function problem.
Here is my code:
<script>
function checkValidity(){
//Create variable to check for errors
var input = document.getElementByID('myID').value;
if(input < 0){
document.getElementByID('errorCheck').textContent = 'The value must be a positive integer';
}else if(ifNaN(input)){
document.getElementByID('errorCheck').textContent = 'Not a number. The value must be a positive integer.';
}else if(input == null){
document.getElementByID('errorCheck').textContent = 'Please input a value.';
} else{
document.getElementByID('errorCheck').textContent = 'Valid number.';
}
}
document.getElementByID('validateid').onclick = checkValidity;
It yells at me at this line of code saying it is invalid.
document.getElementByID('validateid').onclick = checkValidity;
I know its a small mistake. I'd appreciate if someone pointed it out.
You're looking for document.getElementById, lowercase d.
Looks like a typo. You need to do:
document.getElementById('errorCheck')
with lowercase 'd'. Here is a reference link.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
<script>
var currentNumber = 0;
while (currentNumber <= 12) {
alert(currentNumber);
currentNumber = currentNumber + 2;
}
<\script>
I want to prints out all even numbers from 0 to 12 , Why i can get nothing when run it?
It's possible that your ending <script> tag is causing the problem. The slash is pointing the wrong way. It should be </script>, not <\script>.
Try this code
<script>
var currentNumber = 0;
while (currentNumber <= 12) {
alert(currentNumber);
currentNumber = currentNumber + 2;
}
</script>
You're getting a syntax error because the <script> tag is not closed properly. It isn't <\script>, it's </script>. This same pattern applies to all tags, make sure you have the correct slash.
Maybe you can use a text editor that can highlight mistakes like this.
I am using SublimeText and you may have a try.