简体   繁体   中英

java swing validate textfield

I would like to know how I can validate a textfield for blood types.

For example, I have this, but I also need the negative symbol (-). Can somebody can help me?

Example input to validate:

A+ B+ AB+ O+ A- B- AB- O-

Code I have so far:

if(txtSangre.matches("(A|B|AB|O)\\+")==false){    
    mensaje("Ingresar un grupo sanguineo");
}

If you're looking to match one blood type, try this:

if (txtSangre.matches("(A|B|AB|O)(\\+|-)") == false) {    
    mensaje("Ingresar un grupo sanguineo");
}

It will match text A, B, AB, or O followed by either a plus (+) or a minus (-).

If you're looking to match a whole line of data separated by spaces as given in your question, use this regex pattern instead:

if (txtSangre.matches("^(\\s?(A|B|AB|O)(\\+|-)\\s?)+$") == false) {    
    mensaje("Ingresar un grupo sanguineo");
}

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