How to validate a textbox through javascript

Showing Answers 1 - 9 of 9 Answers

Rani

  • Mar 30th, 2007
 

According To my knowledge,
   <form name="form1">                        

  <input type="text" name="text1" onchange="calling()" />

</form>

<script language="javascript">


function calling()

{

      if(document.form1.text1.value=="")
                 alert("please enter the text1 field");

}

</script>

  Was this answer useful?  Yes

jitendra224

  • Apr 12th, 2010
 

Write with inscript
<script type="text/javascript">
function validateFormOnSubmit(theForm) 
{
           var reason = "";
           reason = validateUsername(theForm.name);
}
function validateUsername(fld) 
{
var error = "";
var regex1 = /[0-9]/;
var illegalChars = /[W_]/;
//var regex=/^[a-zA-Z]/;
//var valid = /W/; // allow letters, numbers, and underscores
//var illegalChars = /^.w*w*.+$/; //check alphabetic chars

if (fld.value == "") 
{
                 fld.style.background = 'Yellow'; 
                                  error = "You didn't enter Name.n";
}
  else if ((fld.length < 5) || (fld.length > 15)) 
  {
                  fld.style.background = 'Yellow'; 
                   error = "The Name is the wrong length.n";
  else if (fld.value.match(regex1)) 
  {
                   fld.style.background = 'Yellow'; 
                    error = "Numbers are not allowed in Name.n";
 
  else if (fld.value.match(illegalChars)) 
  {
                   fld.style.background = 'Yellow'; 
                   error = "The Name contains illegal characters.n";
else 
{
                                   fld.style.background = 'White';
}
return error;
}
 
<form  onsubmit="return validateFormOnSubmit(this)" method="GET" >
<table>
<tr>
<TD> Name </TD>
<TD><input type ="text"  name="name" ></html></TD>
</tr>
</table>
<form>      
</script>

  Was this answer useful?  Yes

A textbox can be validated using a JavaScript, by using a redirection to the JavaScript page ..say I need to check for username, then, we redirect the form to login.js (JavaScript for the checking) by the action attribute.

Inside login.js
we can create an array and have something like this
int flag=0;
array[names]={"name1",......};
for(int i=0;i<n;i++)
if(username==array[i])
{
flag=1;
}
if (flag==0)
alert("incorrect username");
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions