4

Now i try to use function "onblur" with the javacript in the input text.

The alert message check is work .

But how to clear input text when the ID Card is wrong (make it empty value after alert "Wrong ID Card")

And also keep value when the ID Card is correct.

Can anyone help me. Thank you so much.

This is my Code

    function checkID(id)
    {
    if(id.length != 13) return false;
    for(i=0, sum=0; i < 12; i++)
    sum += parseFloat(id.charAt(i))*(13-i); if((11-sum%11)%10!=parseFloat(id.charAt(12)))
    return false; return true;}
    
    function checkForm()
    { if(!checkID(document.idform.idcard.value))
    alert('Wrong ID Card');
    document.idform.idcard.value = "";
    }
   <form name="idform">
    ID Card Check: <input type="text" name="idcard" maxlength="13" onblur="checkForm();" />
    </form>
    
    
 <br>
  <p>(Correct ID Card = 1100701342240 ) </p>

2 Answers2

0

It works. When you press OK button on alert message the input field cleans. you need to write one more line in checkForm function

function checkForm(){
    if(!checkID(document.idform.idcard.value)){
     alert('Wrong ID Card');
     document.idform.idcard.value = "";
    }
}
Jitu Raiyan
  • 498
  • 6
  • 13
  • but when the id check is Correct , the value is empty too – Pond Smitty Apr 09 '17 at 16:52
  • I correct this now. I put the line inside if condition too – Jitu Raiyan Apr 09 '17 at 16:58
  • sorry can you explain more, i'm very new for javascript – Pond Smitty Apr 09 '17 at 17:06
  • Can you tell me more about your validation requirement? Then I can solve your problem properly. I didn't understand the mechanism of your checkID function properly. – Jitu Raiyan Apr 09 '17 at 17:12
  • emm.. Now i can make it empty value when it alert "Wrong ID Card" . But cannot keep the value when the ID Card is Right :( http://stackoverflow.com/a/43309641/7642383 – Pond Smitty Apr 09 '17 at 17:22
  • function checkForm(){ if(document.idform.idcard.value != 1100701342240){ alert('Wrong ID Card'); document.idform.idcard.value = ""; } } Here if you input the correct id onblur nothing occars (input value does not clean) and if you put the correct id then onblur the alert message appears and when you click OK, then the value is set to 0. – Jitu Raiyan Apr 09 '17 at 18:16
  • thank you for your suggestion, but the Correct id card is not only 1100701342240 . It just the only one in a million of correct ID Card. – Pond Smitty Apr 09 '17 at 20:10
  • Here, your checkID function evaluate the input correct/wrong by returning true/false. Inside checkID function you add your functionality to evaluate the input (true/false). And checkForm function show the alert message if checkID returns true, otherwise do nothing. It's very simple. Nothing complicated. – Jitu Raiyan Apr 09 '17 at 20:23
0

Finally, I can solved it. Thank you for every comment :)

function checkID(id)
{
if(id.length != 13) return false;
for(i=0, sum=0; i < 12; i++)
sum += parseFloat(id.charAt(i))*(13-i); if((11-sum%11)%10!=parseFloat(id.charAt(12)))
return false; return true;}

function checkForm()
{ if(checkID(document.idform.idcard.value))
return true;
else alert('Wrong ID Card');
document.idform.idcard.value = "";
}
<form name="idform">
ID Card Check: <input type="text" name="idcard" maxlength="13" onchange="checkForm();" required/>
</form>

<br>

<p>Test with one in a million of Correct ID : 1100701342240 </p> <p>If put another random ID it will alert "Wrong ID" </p>