简体   繁体   中英

what is the use of focus function here ? can anyone explain the below code? i'm a beginner so i need to know the use of focus function

Need to know the use of focus function here. Can anyone explain this code?

function Allow Age(){
  if(!form.age.value.match(/[0-9]+$/) && form.age.value !="")
  {
      form.age.value="";
      form.age.focus();
      alert("invalid format");
  }
  if(form.age.value.length > 1)
      alert("invalid entry")
}   

.focus() makes the blinking line appear in the text box so you can start typing in it. Try googling it first in future.

focus() will cause the cursor to show up in that field so when the user types, they type into that field. It is the same as when the user clicks on the field right before typing into it.

The first If statement in this code is to validate that the Age entered by the user is numerical, and not null. The second is used to check if length of entered age is greater than 1 (Not sure why you have this, but this is what your statement does). As for your first question, Focus is used to give the textbox the blinking cursor, ie : Set it ready for receiving user input.

In pseudo code, it is :

function Allow Age(){
If age is not numeric and its value is not null
  Clear the textbox
  Set focus to the textbox
  Send a message saying "Invalid format"
End if

If the length of the age entered is greater than 1
  Send a message saying "Invalid Entry"
End if
}

这意味着form中的age字段将获得输入焦点

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM