Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

text validation

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all
This a simple one that is bothering me for days. I am using this code to validate a textfield
function checkCompCode(value){
sAccInfo = document.mainForm.compCode.value;
len = sAccInfo.length;
if(len<4 || len >6) {
alert("Please enter a 4 to 6 digits value for Component code");
window.document.mainForm.compCode.value='';
}
document.mainForm.compCode.focus();
return;
}
The focus() never works. Any help?
Thanks
srividya
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im new at this but try :
field.focus();
 
kim gick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry ...
put the focus under the alert
 
Srividya Shiv
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does'nt work.
 
Ranch Hand
Posts: 449
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
There are two catches in this script
1. Change your code to
function checkCompCode(){
sAccInfo = document.mainForm.compCode.value;
len = sAccInfo.length;
if(len<5 || len >6) {
alert("Please enter a 4 to 6 digits value for Component code");
window.document.mainForm.compCode.value='';
document.mainForm.compCode.focus();
return;
}
return;
}

2. If you are calling this script in onchange then it focus() dosen't work. Try using onBlur of the text field.
 
Srividya Shiv
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! It worked!
Is there any reason why focus() would work with onBlur and not onChange? I never came across this point.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic