change this:
var name=document.nicform.name.value;
to:
var name = document.getElementById('name');
And submit your form with js\a button\etc..
Your code is a little messed up. I didn't fix it..only made it work. Check this plnkr:
Domanda
I am new to JavaScript and trying to learn it. I just have a text field and want to check if it is empty. I am doing it successfully but now i want to check that if it contains only asterisk (*) in a new JavaScript function! my code is showing the alert box Please help me if i am doing something wrong my code is:
<html>
<head>
<script>
function Verify(){
if(!isNameEmpty()){
return false;
}
if(isNotValidName()==false){
return false;
}
}
function isNameEmpty(){
var name=document.nicform.name.value;
if(name==""){
alert("Please Enter Your Name!");
return false;
}
}
function isNotValidName(){
var name=document.nicform.name.value;
if(name=="*"){
alert("hello star");
return false;
}
}
</script>
<title>
NIC FORM EXAMPLE
</title>
</head>
<body>
<form name="nicform" onsubmit = "return Verify()">
<table border="1" width="400px">
<th>
FILL IN ALL THE FIELDS!
</th>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="name" maxlength="10" size="30">
</td>
</tr>
<tr>
<td>
Age:
</td>
<td>
</td>
</tr>
</table>
</form>
</body>
Soluzione
change this:
var name=document.nicform.name.value;
to:
var name = document.getElementById('name');
And submit your form with js\a button\etc..
Your code is a little messed up. I didn't fix it..only made it work. Check this plnkr:
Altri suggerimenti
It is best to target by ID as mentioned by Amiros.
However, if your form was submitting fine before, perhaps try replacing your JavaScript with this:
function Verify(){
var name=document.nicform.name.value;
if(name==""){
alert("Please Enter Your Name!");
} else if(name=="*"){
alert("hello star");
}
return false;
}